gpt4 book ai didi

dart - Dart:日期添加不一致

转载 作者:行者123 更新时间:2023-12-03 04:20:40 25 4
gpt4 key购买 nike

2020-09-18添加一天时,我得到2020-09-19 00:00:00.000,但是当我添加3天(或更多天)时,我得到2020-09-20 23:00:00.000,哪一个先获得23小时?第二次加3天到18是21而不是20!
问题是什么?

void main() {
var dateOne = DateTime.parse('2020-09-18 00:00:00.000');
var dateTwo = dateOne.add(Duration(days: 1 )); // 2020-09-19 00:00:00.000
var dateThree = dateOne.add(Duration(days: 3 )); // 2020-09-20 23:00:00.000
print(dateTwo);
print(dateThree);
}

最佳答案

原因是Daylight Saving Time,因为add方法只理解API中记录的秒,因此Duration在使用前会转换为秒:

Notice that the duration being added is actually 50 * 24 * 60 * 60 seconds. If the resulting DateTime has a different daylight saving offset than this, then the result won't have the same time-of-day as this, and may not even hit the calendar date 50 days later.

Be careful when working with dates in local time.


https://api.dart.dev/stable/2.7.2/dart-core/DateTime/add.html
正如您自己提到的那样,您可以改用UTC来解决此问题。

关于dart - Dart:日期添加不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62822615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com