flutter - 向日期添加天数无法正常工作
问题描述
我正在尝试将日期添加到日期,但我不明白结果,这是我的代码:
void testDate(){
DateTime start = DateTime(2019,10,1);
DateTime end = DateTime(2019,11,1);
List<DateTime> list = new List();
DateTime current = DateTime.fromMillisecondsSinceEpoch(start.millisecondsSinceEpoch);
while(current.isBefore(end)){
list.add(DateTime.fromMillisecondsSinceEpoch(current.millisecondsSinceEpoch));
Duration duration = Duration(days:3);
current = current.add(duration);
print(current);
current = DateTime(current.year,current.month,current.day);
}
}
我得到了这个结果:
2019-10-04 00:00:00.000
2019-10-07 00:00:00.000
2019-10-10 00:00:00.000
2019-10-13 00:00:00.000
2019-10-16 00:00:00.000
2019-10-19 00:00:00.000
2019-10-22 00:00:00.000
2019-10-25 00:00:00.000
2019-10-27 23:00:00.000
2019-10-29 23:00:00.000
2019-11-01 00:00:00.000
为什么 2019-10-25 00:00:00.000 + 3 天 = 2019-10-27 23:00:00.000 ?应该是 2019-10-28 00:00:00.000
解决方案
该文档指出:
“请注意,添加的持续时间实际上是 50 * 24 * 60 * 60 秒。如果生成的 DateTime 具有与此不同的夏令时偏移量,则结果将不会具有与此相同的时间,并且可能甚至没有达到 50 天后的日历日期。
使用当地时间的日期时要小心。”
可能有一个夏令时开始之间
2019-10-25 00:00:00.000
2019-10-27 23:00:00.000
在以下位置查看:
推荐阅读
- python - 如何将列表的迭代索引传递给多处理池调用的函数?
- javascript - 如何定义类似于getter和setter的deleter
- php - wordpress 插件:使用 oop php 调用未定义的函数 add_menu_page()
- python - nn.Linear 的输出对于相同的输入是不同的
- javascript - 我需要比较两个日期并根据比较结果显示一个图标。但是如果条件失败,我只得到其他结果
- system-font - TinyMCE 显示生成器字体更改标签
- python - 用于查找子集的输入差异
- python - 为什么如果 elif 语句运行两次?
- python - 如何在 Python 中使用 Panda3D 和 Pyinstaller 制作可执行文件?
- php - 通过 Mysql 查询后无法检索状态计数