Comparing only dates of DateTimes in Dart

42,266

Solution 1

Since I asked this, extension methods have been released in Dart. I would now implement option 1 as an extension method:

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

Solution 2

You can use 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');
  }

Solution 3

DateTime dateTime = DateTime.now();
DateTime _pickedDate = // Some other DateTime instance

dateTime.difference(_pickedDate).inDays == 0 // <- this results to true or false

Because difference() method of DateTime return results as Duration() object, we can simply compare days only by converting Duration into days using inDays property

Solution 4

Use instead the package: dart_date Dart Extensions for DartTime

dart_date provides the most comprehensive, yet simple and consistent toolset for manipulating Dart dates.

dart_date

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

Also here the difference in days :

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

Solution 5

I am using this function to calculate the difference in days.

Comparing dates is tricky as the result depends not just on the timestamps but also the timezone of the user.

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

Lucas

Updated on February 21, 2022

Comments

  • Lucas
    Lucas about 2 years

    I need to store and compare dates (without times) in my app, without caring about time zones.
    I can see three solutions to this:

    1. (date1.year == date2.year && date1.month == date2.month && date1.day == date2.day)
      This is what I'm doing now, but it's horrible verbose.

    2. date1.format("YYYYMMDD") == date2.format("YYYYMMDD")
      This is still rather verbose (though not as bad), but just seems inefficient to me...

    3. Create a new Date class myself, perhaps storing the date as a "YYYYMMDD" string, or number of days since Jan 1 1980. But this means re-implementing a whole bunch of complex logic like different month lengths, adding/subtracting and leap years.

    Creating a new class also avoids an edge case I'm worried about, where adding Duration(days: 1) ends up with the same date due to daylight saving changes. But there are probably edge cases with this method I'm not thinking of...

    Which is the best of these solutions, or is there an even better solution I haven't thought of?

  • Komeyl94
    Komeyl94 over 3 years
    actually the correct way to get the difference is this: a.difference(b).inDays
  • Nilesh Rathore
    Nilesh Rathore over 3 years
    In the extension function, you can remove this to access its members. The function can be. bool isSameDate(DateTime other) { return year == other.year && month == other.month && day == other.day; }
  • Marco
    Marco about 3 years
    This is not correct. Two dates could differ of less than a day, but still be two different days (e.g. date1 = 27/04/2021 23:00 - date2 = 28/04/2021 01:00)
  • Alexey Inkin
    Alexey Inkin over 2 years
    Note that in practice dates most probably differ in days, then months, and then years. So starting with day should be faster.
  • A5H1Q
    A5H1Q almost 2 years
    this compares time also
  • holocronweaver
    holocronweaver almost 2 years
    @Komeyl94 As mentioned in comments for a similar answer, a.difference(b).inDays is incorrect and will fail when DateTimes are less than 24 hours apart (e.g. date1 = 27/04/2021 23:00 - date2 = 28/04/2021 01:00).