Flutter, Unable to convert string to date - flutter

I'd like to convert a string to Date but I get this error when display date in Text :
Trying to read / from 2020-04-18 19:43:43.755927 at position 5
and this is the function
String get dateNote {
var d = DateFormat("yyyy/MM/dd", "en_US").parse(createdAt);
return d.toString();
}

final String date = '2020-04-18 19:43:43.755927';
String getFormattedDate(String date) {
var d = DateTime.parse(date);
return [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
][d.month - 1] +
" " + d.day.toString() +
"," +
d.year.toString();
}
print(getFormattedDate(date));
Output : Apr 18,2020

I found a short solution:
String get dateNote {
var date = DateTime.parse(createdAt);
var strDate = DateFormat("yyyy-MM-dd").format(date).toString();
return strDate;
}

Related

google script type error is not a function in date function

can someone help me figure what's wrong with my logic?, i'm new using google script app and stuck around a week and got this error
Error message:
TypeError: d1.getTime is not a function (inDays)
var DateDiff = {
inDays: function(d1, d2) {
var t2 = d2.getTime();
var t1 = d1.getTime();
Logger.log("t1" + t1)
return parseInt((t2-t1)/(24*3600*1000)+1);
}
}
var dateStr = data[m][xMonth].toString() // the value here is =>1/10/2022
var todayDate = new Date();
if(dateStr.toString().includes('/')){
var yearA = '20'+dateStr.substring(6, 8)
var monthA = +dateStr.substring(3, 5)
if(monthA.toString() != '11' || monthA.toString() != '12') monthA = '0'+monthA
var dayA = +dateStr.substring(0, 2)
var tempDate = yearA + monthA + dayA
var Ryear = +tempDate.substring(0, 4)
var Rmonth = +tempDate.substring(4, 6)
var Rday = +tempDate.substring(6, 8)
var newDate = new Date(Ryear, Rmonth - 1, Rday)//because month start from 0
var realDueDate = DateDiff.inDays(todayDate, newDate) // error here in var newDate
}
Try this:
function myFunk() {
const D2 = new Date();
const D1 = new Date(D2.getFullYear(),D2.getMonth() - 2,D2.getDate());
var DateDiff = {
d1:D1,d2:D2,
inDays: function () {
let t2 = this.d2.getTime();
let t1 = this.d1.getTime();
return parseInt((t2 - t1) / (24 * 3600 * 1000) + 1);
}
}
Logger.log(DateDiff.inDays());
}
Execution log
10:58:41 AM Notice Execution started
10:58:39 AM Info 62.0
10:58:41 AM Notice Execution completed

DateFormat in flutter

How to get the hour, minute and am/pm seperately using the bellow format in flutter?
The below is the piece of code i tried,
DateTime now = DateTime.now();
var formatTime = DateFormat("hh:mm a").format(now);
import 'package:intl/intl.dart';
void main(List<String> arguments) {
var now = DateTime.now();
var formatTime = DateFormat('hh:mm a').format(now);
//you can get hour minute directly
var hhDirect = now.hour;
var mmDirect = now.minute;
//or your can get with this method
var splitTimeAndAmPm = formatTime.split(' ');
var splitHHandMM = splitTimeAndAmPm.first.split(':');
var am_pm = splitTimeAndAmPm.last;
var hh = splitHHandMM.first;
var mm = splitHHandMM.last;
print('HH Direct: ' + hhDirect.toString());
print('MM Direct: ' + mmDirect.toString());
print('HH: ' + hh);
print('MM: ' + mm);
print('AmPm: ' + am_pm);
}

How to get Weekly dates list in dart logic

Can any one help me out to getting list of dates of each week in a given month with selection of start of weekday like monday or any other .
not able figure out the logic in flutter (Dart)
for example :
input :
Month : May
weekday : Monday
output :
[
{
"week1": [
"-",
"-",
"-",
"-",
"2020-05-01",
"2020-05-02",
"2020-05-03"
]
},
{
"week2": [
"2020-05-04",
"2020-05-05",
"2020-05-06",
"2020-05-07",
"2020-05-08",
"2020-05-09",
"2020-05-10"
]
},
{
"week3": [
"2020-05-11",
"2020-05-12",
"2020-05-13",
"2020-05-14",
"2020-05-15",
"2020-05-16",
"2020-05-17"
]
},
{
"week4": [
"2020-05-18",
"2020-05-19",
"2020-05-20",
"2020-05-21",
"2020-05-22",
"2020-05-23",
"2020-05-24"
]
},
{
"week5": [
"2020-05-25",
"2020-05-26",
"2020-05-27",
"2020-05-28",
"2020-05-29",
"2020-05-30",
"2020-05-31"
]
}
]
Finally i have found the solution . posting here for future use of others
calculateWeeks(DateTime currentMonth, DateTime previousMonth) {
List<List<String>> weeks = [];
currentMonth = DateTime.utc(2020, DateTime.august, 1);
if (currentMonth == null) {
currentMonth = DateTime.now();
}
int firstDay = 1;
int lastDay = DateUtils.daysofMonth(currentMonth);
log('Day from $firstDay - $lastDay');
DateTime startMonth =
DateTime.utc(currentMonth.year, currentMonth.month, firstDay);
DateTime endMonth =
DateTime.utc(currentMonth.year, currentMonth.month, lastDay);
DateTime weekStartDates = DateUtils.weekStart(startMonth);
DateTime weekEndDates = DateUtils.weekEnd(endMonth);
int daysDiff = weekEndDates.difference(weekStartDates).inDays;
log('Start week : $weekStartDates');
log('End week : $weekEndDates');
log('Diff : ${daysDiff}');
List<String> weekly = [];
for (int i = 0; i < daysDiff; i++) {
String date = '';
DateTime checkdate = weekStartDates.add(Duration(days: i));
if ((checkdate == startMonth || checkdate.isAfter(startMonth)) &&
(checkdate == endMonth || checkdate.isBefore(endMonth))) {
log('weekday : $i - $checkdate : ${checkdate.weekday}');
date = checkdate.toIso8601String();
}
weekly.add(date);
if (checkdate.weekday == 7 || i == daysDiff - 1) {
weeks.add(weekly);
weekly = [];
}
}
log('Weekly Dates :${json.encode(weeks)}');
}

How to get the names of days between two dates ionic+angularjs

After using this function to get the number of days I want to get the names of days between two dates ionic+angularjs.
$scope.dayDiff = function (date1, date2) {
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
$scope.dayDifference = Math.ceil(timeDiff / (1000 * 3600 * 24));
alert($scope.dayDifference);
}
Here's how I did it. View Plunkr Demo
angular.module('app', [])
.controller('MainCtrl', function($scope) {
var days = function(date1, date2) {
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diff = Math.ceil(timeDiff / (1000 * 3600 * 24));
var _days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var days = [];
for (var i = 0; i < diff; i++) {
date1.setDate(date1.getDate() + i);
days.push(_days[date1.getDay()]);
}
return days;
};
$scope.days = days(new Date(2016, 05, 01), new Date(2016, 05, 05));
})

Inserting Date&Time on Google Sheets

On Google Sheets;
I want it to display Day/Month/Year & Hour/Minute
When I click Insert date it shows 6/20/2015 21:59:4
I want it to show 20/06/2015 21:59
I have tried a few things and I managed to change the month/day around but it threw up errors so had to start again.
If anyone has better code or can fix this code It would help alot!
function getDate() {
var d = new Date();
return (d.getMonth()+1) + "/" + d.getDate() + "/" + d.getFullYear();
}
function getTime() {
var d = new Date(),
offset = -d.getTimezoneOffset()/60,
h = d.getUTCHours() + offset,
m = d.getMinutes(),
s = d.getSeconds();
return h + ":" + m + ":" + s;
}
function getDateAndTime() {
return getDate() + " " + getTime();
}
function insertValue(theValue, format) {
var ss = SpreadsheetApp.getActiveSpreadsheet(),
theSheet = ss.getActiveSheet(),
theCell = theSheet.getActiveSelection();
theCell.setNumberFormat(format || theCell.getNumberFormat());
theCell.setValue(theValue);
return true;
}
function getValue() {
return SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getActiveCell().getValue();
}
function getFormat() {
return SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getActiveCell().getNumberFormat();
}
function insertDate() {
insertValue(getDate(), "Date");
}
function insertTime() {
insertValue(getTime(), "Time");
}
function insertDateAndTime() {
insertValue(getDateAndTime(), "Date time");
}
function appendDate() {
insertValue(getValue() + " " + getDate());
}
function appendTime() {
insertValue(getValue() + " " + getTime());
}
function appendDateAndTime() {
insertValue(getValue() + " " + getDateAndTime());
}
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [];
menuEntries.push({name: "Insert Date", functionName: "insertDate"});
menuEntries.push({name: "Insert Time", functionName: "insertTime"});
menuEntries.push({name: "Insert Date and Time", functionName: "insertDateAndTime"});
menuEntries.push(null); // line separator
menuEntries.push({name: "Append Date", functionName: "appendDate"});
menuEntries.push({name: "Append Time", functionName: "appendTime"});
menuEntries.push({name: "Append Date and Time", functionName: "appendDateAndTime"});
ss.addMenu("Insert Date", menuEntries);
}
function onInstall() { onOpen(); }
does this formula work as you want:
=TEXT(NOW(),"dd/mm/yyyy hh:mm")
Maybe use Utilities.formatDate().
See here for more info
example:
var formattedDate = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "dd/MM/yyyy HH:mm");