TemporalDate from DateTime - Flutter - flutter

I am using AWS amplify for my project and trying to save the selected date from datepicker to the model.
DatePicker is giving the date in DateTime object, when i try to convert it to TemporalDate its giving me a day before date. i.e if i select todays date and assigning it to the TemporalDate then i am getting yesterday's date.
Here is my code :
var d1 = TemporalDate(pickedDate);
var d4 = TemporalDate(pickedDate.toUtc());
var d2 = TemporalDate(pickedDate.toLocal());
var d3 = TemporalDate(pickedDate).getDateTime();
pickedDate is holding today's date but the d1,d2,d3,d4 all is having yesterday's date.
Expected result is to get the today's date in the TemporalDate variable.

You need to parse the date. It is showing a day before because it is on the universal date. So you just need to convert the universal date to local date.
var dateTime = DateFormat("yyyy-MM-dd HH:mm:ss").parse(selectedDate.toString(), true);
var dateLocal = dateTime.toLocal();

Related

flutter datetime how to convert it

I have the following datetime from a database: 2022-10-02T23:10:24.736Z I want to compare it to a datetime now and get the difference in second. Basically I have the code for it, but im not able to convert the datetime.now to the mentioned format. It tells me it has a difference of 122 minutes but it should only be 2 minutes..
DateTime datenow = DateTime.now();
DateTime dateFormDatabase = DateTime.parse(widget.data[widget.currIndex-1]["roundEnd"]);
print(datenow); // <---- 2022-10-01 23:22:45.522687
print(dateFormDatabase); // <--- 2022-10-01 23:25:24.736Z
var diff = dateFormDatabase.difference(datenow);
print(diff.inMinutes); // <--- 122 but it should be 2minutes
The returned difference is correct. You are probably looking at different timezones here. DateTime.now() returns the current date and time in your local timezone. The parsed date from your database seems to be coming in as UTC. Notice the "Z" at the end here: 2022-10-01 23:25:24.736Z.
If you want to visually compare them you could convert either of those to UTC/local with toUtc or toLocal

Auto-populate today's date if no date entered

I currently have a start date in cell C1 and end date in C2 of a Google Sheet spreadsheet. I would like to set it up so that if no date is entered in the end date (C2), this cell will be auto-populated with today's date
I have thus far found the following script
function onFormSubmit(e) {
//edit responses sheet name
var responseSheetName = 'Stats';
//Edit colmn number, column in which the date has to be autopopulated
var column = 3;
//Get target row number
var row = e.range.rowStart;
//If no date, pouplate the cell with current date
if(!e.values[column-1]){
SpreadsheetApp.getActive().getSheetByName(responseSheetName).getRange(row, column).setValue(new Date())
}
}
This doesn't seem to be doing the trick so either I am reading it wrong, it is not what I am looking for!
Is this something that is possible?

Add Days Google Script

I wondered if anyone could help. I have a script where I am pulling out data from a spreadsheet list, where this is a match for this week (basically an events list, to produce a weekly agenda). I will use a for loop to increment the days to add on, but I am just trying to make it work for one day for now...
The first column is the data in format dd/mm/yyy
I am trying to take today's increment by 1 and then search through the list to find a match. The searching etc, I can make work, but the date part is just not playing. I wondered if anyone could advise.
E.g. Date Column A:
06/07/2021
06/07/2021
01/11/2021
01/11/2021
01/11/2021
01/11/2021
02/09/2021
02/09/2021
var selectedDate = row[0];
selectedDate = Utilities.formatDate(new Date(selectedDate), "GMT+1", "dd/MM/yyyy");
var currdate = new Date();
currdate = Utilities.formatDate(new Date(selectedDate), "GMT+1", "dd/MM/yyyy");
var daystochange = 1;
var newdate = new Date(currdate.getFullYear, currdate.getMonth, currdate.getDay+daystochange );
Could anyone help?
Thanks
Only use Utilities.formatDate() to output dates, not to work with dates.
The JavaScript date object has all you need to work with dates and compare. When you use the Utilities function it converts it to a string, and so you lose all the functionality of the Date object.
Also bear in mind that if you have dates, that are formatted as dates in your sheet, they will automatically be returned as Date objects.
For example, if your sheet has a date in cell A1
var date = Sheet.getRange("A1").getValue()
date instanceof Date // true
Once you have your date, if you want to add one day to it, you can take an approach similar to what you have already done:
var selectedDate = new Date(2021, 1, 15)
var newdate = new Date(selectedDate.getFullYear(), selectedDate.getMonth(), selectedDate.getDate() + 1);
console.log(newdate) // Tue Feb 02 2021 00:00:00
Note - use getDate to return the day of the month, getDay only returns day of the week.
To check if two dates are the same, you can write a function to compare:
function isSameDate(a, b) {
return a instanceof Date &&
b instanceof Date &&
a.getYear() === b.getYear() &&
a.getMonth() === b.getMonth() &&
a.getDate() === b.getDate()
}
This function will return true if the dates are the same.
Reference
Date

How to convert correctly only time in dart

i've a string like this
String time = '13:00:00Z'; (Zulu time)
I want to convert it into a string in the correct local time.
For example if i execute the program, i want 15:00 as a result because 13:00 in my local time it's 15:00.
I get the error "FormatExeption: Invalid date format", whatever I put in the DateFormat
You can prepend current date to the time string and then use toLocal() to convert it to your local time:
DateTime date = DateTime.now();
String formattedDate = DateFormat('yyyy-MM-dd').format(date);
String time = "13:00:00Z";
DateTime dt = DateTime.parse(formattedDate + "T" + time).toLocal();
String formattedTime = DateFormat('HH:mm').format(dt);
print(formattedTime);
(DateFormat is from the intl package)

MomentJS date string adds one day

I don't understand why this date is saved as +1 day:
startdate = "2017-11-29T23:59:59.999Z";
var new_date = moment(startdate).format('DD/MM/YYYY'); // --> gives 30/11/2017
But if I do:
startdate = "2017-11-29";
var new_date = moment(startdate).format('DD/MM/YYYY'); // --> gives the correct date 29/11/2017
Any ideas?
Here is a jsfiddle showing this: http://jsfiddle.net/jbgUt/416/
Thanks!
If a time part is included, an offset from UTC can also be included as +-HH:mm, +-HHmm, +-HH or Z.
Add utc() to avoid it.
moment(startdate).utc().format('DD-MM-YYYY')
or
moment.utc(startdate).format('DD-MM-YYYY')
If you want to parse or display a moment in UTC, you can use moment.utc() instead of moment()
Late to the party on this one, but I did just convert a few of our product's date-time objects to https://moment.github.io/luxon/
Takes out the need for the .utc() method above.