gpt4 book ai didi

dart - dart DateTime.add从特定日期增加1小时

转载 作者:行者123 更新时间:2023-12-03 04:51:44 24 4
gpt4 key购买 nike

我会有一个 Dart 问题。

print(DateTime(2020,03,12).add(new Duration(days: 17)));
print(DateTime(2020,03,12).add(new Duration(days: 18)));

结果:

2020-03-29 00:00:00.000

2020-03-30 01:00:00.000

我不明白第二个结果。为什么是01:00:00?

这是在Flutter测试和dartpad.dev上在线运行的结果,尽管如果我从flutter应用程序运行它,它会正确显示00:00:00。为什么?
print(DateTime(2019,03,12).add(new Duration(days: 17)));

print(DateTime(2019,03,12).add(new Duration(days: 18)));

显示:

2019-03-29 00:00:00.000

2019-03-30 00:00:00.000
print(DateTime(2021,03,12).add(new Duration(days: 17)));

print(DateTime(2021,03,12).add(new Duration(days: 18)));

显示:

2021-03-29 01:00:00.000

2021-03-30 01:00:00.000

最佳答案

原因是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

关于dart - dart DateTime.add从特定日期增加1小时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61235834/

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