我一直在做一个扑扑的项目,并在发现以下行为时尝试按特定日期过滤结果。
main() {
String v = '2020-08-03';
int ms0 = 1596249000000;
int ms1 = 1596368040000; //
int ms2 = 1596465736799;
int ms3 = 1596472120778; //
int ms4 = 1596623965588;
print(DateTime.fromMillisecondsSinceEpoch(ms0));
print(DateTime.fromMillisecondsSinceEpoch(ms1));
print(DateTime.fromMillisecondsSinceEpoch(ms2));
print(DateTime.fromMillisecondsSinceEpoch(ms3));
print(DateTime.fromMillisecondsSinceEpoch(ms4));
print(DateTime.parse(v).difference(DateTime.fromMillisecondsSinceEpoch(ms0)).inDays);
print(DateTime.parse(v).difference(DateTime.fromMillisecondsSinceEpoch(ms1)).inDays);
print(DateTime.parse(v).difference(DateTime.fromMillisecondsSinceEpoch(ms2)).inDays);
print(DateTime.parse(v).difference(DateTime.fromMillisecondsSinceEpoch(ms3)).inDays);
print(DateTime.parse(v).difference(DateTime.fromMillisecondsSinceEpoch(ms4)).inDays);
}
上面的代码产生以下结果
2020-08-01 10:30:00.000
2020-08-02 19:34:00.000
2020-08-03 22:42:16.799
2020-08-04 00:28:40.778
2020-08-05 18:39:25.588
1
0
0
-1
-2
我不明白变量“ ms1”和“ ms2”将如何产生相同的差异“ inDays”。有人可以帮忙提供一些指示吗?谢谢大家。
答案 0 :(得分:3)
一天的总时长为24小时,整个小时为60分钟
如果差异少于24小时1秒或1微秒,依此类推-则不计算整天
回到您的示例
2020-08-03
表示一天的开始00:00:00.000
并且不同于
2020-08-02 19:34:00.000
仅4h,26m
2020-08-03 22:42:16.799
仅停留22h,42m,16s,799ms
此测试说明
test('datetime difference in days', () {
final date = DateTime.parse('2020-08-03');
final date1 = DateTime.fromMillisecondsSinceEpoch(1596368040000);
final date2 = DateTime.fromMillisecondsSinceEpoch(1596465736799);
print(date); // 2020-08-03 00:00:00.000
print(date1); // 2020-08-02 17:34:00.000
print(date2); // 2020-08-03 20:42:16.799
expect(date1.difference(date).inDays, equals(0));
expect(date1.difference(date).inHours, equals(-6));
expect(date2.difference(date).inDays, equals(0));
expect(date2.difference(date).inHours, equals(20));
});