Playing in Flutter, using Dart - I am trying to establish the amount of seconds between two dates.
But for some reason the date1.difference(date2).inSeconds
gives a result that does not make sense to me. Maybe it is late and I am too tired to miss something here:
Here is my code: (ie print statements of the dates and its supposed difference in seconds):
print(myDate); // DateTime type
print(queryDate); // DateTime type
print(myDate.difference(queryDate).inSeconds);
And the print-results say:
2019-02-01 00:18:00.000Z // myDate
2019-02-01 01:17:18.859431 // queryDate
41 // supposedly difference in seconds...
But shouldn't it be much more than 41 seconds ????
Could the reason be the x.000Z vs. .859431 format differences ? And if yes, why ?
Why is the difference
method ignoring minutes and hours ?
I found a solution:
Turns out, the queryDate
was in another format (not sure what .859431 means - maybe somebody can explain ...?)
At least, when I do the following, it works:
DateTime queryDate2 = DateTime.utc(
queryDate.year,
queryDate.month,
queryDate.day,
queryDate.hour,
queryDate.minute,
queryDate.second);
Then my print-statements are:
print(myDate); // DateTime type in UTC
print(queryDate); // DateTime type in .859431 format (??)
print(queryDate2); // DateTime type in UTC
print(myDate.difference(queryDate2).inHours);
print(myDate.difference(queryDate2).inMinutes);
print(myDate.difference(queryDate2).inSeconds);
And the print-results say:
2019-02-01 00:18:00.000Z // myDate
2019-02-01 02:01:21.081575 // queryDate
2019-02-01 02:01:21.000Z // queryDate2
-1 // difference in hours (now correct !)
-103 // difference in minutes (now correct !)
-6201 // difference in seconds (now correct !)
Yes, more like it :) (..now hours, minutes and seconds are correct).
Maybe there is another way of changing a date to UTC ? Any idea appreciated.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.