Comparing only dates of DateTimes in Dart

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


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

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;
  }
}