检查datetime变量是今天、明天还是昨天

检查datetime变量是今天、明天还是昨天,datetime,dart,Datetime,Dart,我不知道如何检查datetime变量是今天、明天还是昨天 我没有在类成员中找到方法 final now = DateTime.now(); final today = DateTime(now.year, now.month, now.day); final yesterday = DateTime(now.year, now.month, now.day - 1); final tomorrow = DateTime(now.year, now.month, now.day + 1); f

我不知道如何检查datetime变量是今天、明天还是昨天

我没有在类成员中找到方法

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


final dateToCheck = ...
final aDate = DateTime(dateToCheck.year, dateToCheck.month, dateToCheck.day);
if(aDate == today) {
  ...
} else if(aDate == yesterday) {
  ...
} else(aDate == tomorrow) {
  ...
}

点击:
now.day-1
now.day+1
适用于不同年份或月份的日期。

虽然上述答案是正确的,但我想提供一个更紧凑、更灵活的替代方案:

///返回提供的日期与今天之间的差值(以整天为单位)。
int计算差异(日期时间日期){
DateTime now=DateTime.now();
返回日期时间(date.year,date.month,date.day).difference(DateTime(now.year,now.month,now.day)).inDays;
}
因此,如果要检查
date
是否为:

  • 昨天
    计算差异(日期)=-1
  • 今天
    计算差异(日期)=0
  • 明天
    计算差异(日期)==1

这个也可以

 String checkDate(String dateString){

   //  example, dateString = "2020-01-26";

   DateTime checkedTime= DateTime.parse(dateString);
   DateTime currentTime= DateTime.now();

   if((currentTime.year == checkedTime.year)
          && (currentTime.month == checkedTime.month)
              && (currentTime.day == checkedTime.day))
     {
        return "TODAY";

     }
   else if((currentTime.year == checkedTime.year)
              && (currentTime.month == checkedTime.month))
     {
         if((currentTime.day - checkedTime.day) == 1){
           return "YESTERDAY";
         }else if((currentTime.day - checkedTime.day) == -1){
            return "TOMORROW";
         }else{
            return dateString;
         }

     }

 }

一个简单的
isToday
检查:

///如果[日期]是今天,则返回'true'。尊重当地环境。
/// https://stackoverflow.com/a/60213219/1321917
bool isToday(日期时间日期){
final now=DateTime.now();
最终差异=现在。差异(日期)。因天;
return diff==0&&now.day==date.day;
}

使用dart扩展有助于使代码更加优雅。可以使用以下内容创建实用工具类:

extension DateHelpers on DateTime {
  bool isToday() {
    final now = DateTime.now();
    return now.day == this.day &&
        now.month == this.month &&
        now.year == this.year;
  }

  bool isYesterday() {
    final yesterday = DateTime.now().subtract(Duration(days: 1));
    return yesterday.day == this.day &&
        yesterday.month == this.month &&
        yesterday.year == this.year;
  }
}
然后,当您需要知道一天是今天还是昨天时,将实用程序类导入到您需要它的文件中,然后调用相应的函数,就像它内置在
DateTime
类中一样

Text(
    myDate.isToday() ? "Today" 
  : myDate.isYesterday() ? "Yesterday" 
  : DateFormat("dd MMM").format(myDate)
)
单行代码:)


对于
DateFormat
使用
import'package:intl/intl.dart'

这有一个缺陷。如果“您正在检查的日期”和“今天的日期”的差异小于24小时,则会告诉您差异为0天,这是正确的,但会产生误导。日期不同,但时间间隔不超过一天。通过创建一个只包含年、月和日的新日期时间,我们不考虑时间。这种方法还有一个例外。如果这两个日期来自不同的时区,则差分功能将不考虑这一点。在处理服务器持久性的UTC日期和设备上的本地时区日期时,比较是不正确的。非常感谢“提示:now.day-1和now.day+1适用于不同年份或月份的日期。”这对我来说非常有效!!工作起来很有魅力!它可以是一个getter而不是一个函数,因为我们不传递参数,但总的来说,这是最好的答案
Text(
  date.isAfter(DateTime.now().subtract(Duration(days: 1)))
      ? 'Today'
      : DateFormat('EE, d MMM, yyyy').format(date),
),