[英]Date difference in Dart for Flutter
Playing in Flutter, using Dart - I am trying to establish the amount of seconds between two dates. 使用Dart在Flutter中播放-我正在尝试确定两个日期之间的秒数。
But for some reason the date1.difference(date2).inSeconds
gives a result that does not make sense to me. 但是由于某种原因,
date1.difference(date2).inSeconds
给出的结果对我来说没有意义。 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 ???? 但这不应该超过41秒吗?
Could the reason be the x.000Z vs. .859431 format differences ? 原因可能是x.000Z与.859431的格式差异吗? And if yes, why ?
如果是,为什么?
Why is the difference
method ignoring minutes and hours ? 为什么
difference
方法忽略了分钟和小时?
I found a solution: 我找到了解决方案:
Turns out, the queryDate
was in another format (not sure what .859431 means - maybe somebody can explain ...?) 原来,
queryDate
是另一种格式(不确定.859431是什么意思-也许有人可以解释...?)
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 ? 也许还有另一种将日期更改为UTC的方法? Any idea appreciated.
任何想法表示赞赏。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.