【问题标题】:Comparing only dates of DateTimes in Dart在 Dart 中仅比较 DateTimes 的日期
【发布时间】:2018-10-24 21:32:09
【问题描述】:

我需要在我的应用中存储和比较日期(不包括时间),而不关心时区。
我可以看到三个解决方案:

  1. (date1.year == date2.year && date1.month == date2.month && date1.day == date2.day)
    这就是我现在正在做的事情,但是太冗长了。

  2. date1.format("YYYYMMDD") == date2.format("YYYYMMDD")
    这仍然相当冗长(虽然没有那么糟糕),但对我来说似乎效率低下......

  3. 自己创建一个新的 Date 类,可能将日期存储为“YYYYMMDD”字符串,或自 1980 年 1 月 1 日以来的天数。但这意味着重新实现一大堆复杂的逻辑,比如不同的月份长度,加/减和闰年。

创建一个新类还可以避免我担心的极端情况,由于夏令时更改,添加Duration(days: 1) 最终会以相同的日期结束。但是我没有想到这种方法可能存在边缘情况......

这些解决方案中哪个是最好的,或者有没有我没想到的更好的解决方案?

【问题讨论】:

    标签: date time dart flutter


    【解决方案1】:

    既然我问了这个,extension methods 已经在 Dart 中发布了。我现在将选项 1 实现为扩展方法:

    extension DateOnlyCompare on DateTime {
      bool isSameDate(DateTime other) {
        return year == other.year && month == other.month
               && day == other.day;
      }
    }
    

    【讨论】:

    • 在扩展功能中,您可以删除它以访问其成员。功能可以。 bool isSameDate(DateTime other) { return year == other.year && month == other.month && day == other.day; }
    • 请注意,在实践中,日期很可能以天、月和年为单位。所以从一天开始应该更快。
    【解决方案2】:
    DateTime dateTime = DateTime.now();
    DateTime _pickedDate = // Some other DateTime instance
    
    dateTime.difference(_pickedDate).inDays == 0 // <- this results to true or false
    

    由于DateTime的difference()方法返回的结果是Duration()对象,所以我们可以通过inDays属性将Duration转换为天数来简单比较天数

    【讨论】:

    • 这是不正确的。两个日期可能相差不到一天,但仍然是两个不同的日期(例如 date1 = 27/04/2021 23:00 - date2 = 28/04/2021 01:00)
    【解决方案3】:

    你可以使用compareTo:

      var temp = DateTime.now().toUtc();
      var d1 = DateTime.utc(temp.year,temp.month,temp.day);
      var d2 = DateTime.utc(2018,10,25);     //you can add today's date here
      if(d2.compareTo(d1)==0){
        print('true');
      }else{
        print('false');
      }
    

    【讨论】:

      【解决方案4】:

      改用包:dart_date DartTime 的 Dart 扩展

      dart_date 为操作 Dart 日期提供了最全面、最简单、最一致的工具集。

      dart_date

      DateTime now = DateTime.now();
      DateTime date = ....;
      if (date.isSameDay(now)) {
        //....
      } else {
        //....
      }
      

      这里还有天数的差异:

      int differenceInDays(DateTime a, DateTime b) => a.differenceInDays(b);
      

      【讨论】:

      • 其实获取差异的正确方法是这样的:a.difference(b).inDays
      【解决方案5】:

      我正在使用这个函数来计算天数的差异。

      比较日期很棘手,因为结果不仅取决于时间戳,还取决于用户的时区。

      int diffInDays (DateTime date1, DateTime date2) {
          return ((date1.difference(date2) - Duration(hours: date1.hour) + Duration(hours: date2.hour)).inHours / 24).round();
      }
      

      【讨论】:

        【解决方案6】:

        使用isAtSameMomentAs

        var date1 = DateTime.now();
        var date2 = date1.add(Duration(seconds: 1));
        
        var isSame = date1.isAtSameMomentAs(date2); // false 
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-12-31
          • 1970-01-01
          • 2011-02-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多