gpt4 book ai didi

flutter - 如何确定哪个DateTime大于或小于另一个

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

它应该如何工作:

确保今天今天的 2020-April-01(timestampValidFrom-firebase时间戳格式) 2020-April-05(timestampValidTo)之间。

我需要这样的查询。

1)

timestampValidFrom <= today >= timestampValidTo

要么

2)
 .where('timestampValidFrom', isGreaterThanOrEqualTo: Timestamp.now())
.where('timestampValidTo', isLessThanOrEqualTo: Timestamp.now())

我已经尝试解决此解决方案,但是它不起作用。

DateTime now = DateTime.now();
DateTime yesterday,tomorrow;

yesterday = DateTime(now.year, now.month, now.day); // today 12.00.00
tomorrow = DateTime(now.year, now.month, now.day +1); //tomorrow 12.00.00

if(yesterday.isAfter(snapshot.data[index].timestampValidFrom.toDate())
|| snapshot.data[index].timestampValidFrom.toDate().isBefore(tomorrow)
&& yesterday.isBefore(snapshot.data[index].timestampValidTo.toDate())
|| snapshot.data[index].timestampValidTo.toDate().isAfter(tomorrow)) {
// show widget
} else {
//emplty widget
}

最佳答案

使用difference方法。

final yesterday = DateTime(now.year, now.month, now.day); // today 12.00.00
final tomorrow = DateTime(now.year, now.month, now.day +1); //tomorrow 12.00.00

int diffInDays = tomorrow.difference(yesterday).inDays;
if (diffInDays == 0){
//custom code
print( "same day");
} else if( diffInDays > 0 ) {
// custom code
print("tomorrow is greater ");
} else{
// custom code
print(" yesterday is less" );
}

希望能帮助到你!

关于flutter - 如何确定哪个DateTime大于或小于另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61124885/

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