skip to Main Content

I’m working on a Flutter project where I need to compare two DateTime objects, but only by their date. The time component of the DateTime objects is not relevant for my comparison.

Here’s an example of the DateTime objects I’m dealing with:

Dart

DateTime dt1 = DateTime.parse("2023-11-10 11:47:00");
DateTime dt2 = DateTime.parse("2023-11-10 10:09:00");

I want to compare dt1 and dt2 such that only the date (year, month, day) is considered, not the time. How can I achieve this in Flutter? Any help would be appreciated. Thanks!

2

Answers


  1. Chosen as BEST ANSWER

    In Flutter, you can compare two DateTime objects with respect to only the date (not time) by creating new DateTime objects from the original ones that contain only the year, month, and day information. Here's an example:

    DateTime dt1 = DateTime.parse("2023-11-10 11:47:00");
    DateTime dt2 = DateTime.parse("2023-11-10 10:09:00");
    
    // Create new DateTime objects with only year, month, and day
    DateTime date1 = DateTime(dt1.year, dt1.month, dt1.day);
    DateTime date2 = DateTime(dt2.year, dt2.month, dt2.day);
    
    // Now you can compare date1 and date2
    if(date1.compareTo(date2) == 0){
      print("Both dates are the same.");
    } 
    if(date1.compareTo(date2) < 0){
      print("date1 is before date2");
    } 
    if(date1.compareTo(date2) > 0){
      print("date1 is after date2");
    }
    

    In this code, date1 and date2 are new DateTime objects created from dt1 and dt2 respectively, but they only contain the year, month, and day information. The time is set to 00:00:00 by default. Therefore, when you compare date1 and date2, you're effectively comparing the dates only, not the times.


  2. 1) Creating new Date objects using only year, month, and day attributes

    final date1 = DateTime.parse('2021-09-01 03:30:00');
    final date2 = DateTime.parse('2021-09-01 04:30:00');
    
    int compareOnlyByDate(DateTime other) {
      return DateTime(date1.year, date1.month, date1.day).compareTo(
        DateTime(other.year, other.month, other.day),
      );
    }
    
    // usage
    final result = date1.compareOnlyByDate(date2);
    
    if (result == 0) {
      print('dates are equal');
    } 
    if (result < 0) {
      print('date1 is before date2');
    }
    if (result > 0) {
      print('date1 is after date2');
    }
    

    Or as an extension method:

    extension CompareDates on DateTime {
      int compareOnlyByDate(DateTime other) {
        return DateTime(year, month, day).compareTo(
          DateTime(other.year, other.month, other.day),
        );
      }
    }
    

    2) If you don’t want to create new objects

    You can compare year, month, and day attributes of your DateTime objects:

    int compareOnlyByDate(DateTime date1, DateTime date2) {
      if (date1.year == date2.year) {
        if (date1.month == date2.month) {
          return date1.day - date2.day;
        } else {
          return date1.month - date2.month;
        }
      } else {
        return date1.year - date2.year;
      }
    }
    

    This is another way to do it:

    bool isSameDay(DateTime date1, DateTime date2) {
      return date1.year == date2.year &&
          date1.month == date2.month &&
          date1.day == date2.day;
    }
    
    bool isGreater(DateTime date1, DateTime date2) {
      return date1.isAfter(date2) && !isSameDay(date1, date2);
    }
    
    bool isLess(DateTime date1, DateTime date2) {
      return date1.isBefore(date2) && !isSameDay(date1, date2);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search