Comparing only dates of DateTimes in Dart Comparing only dates of DateTimes in Dart flutter flutter

Comparing only dates of DateTimes in Dart


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 this.year == other.year && this.month == other.month           && this.day == other.day;  }}


DateTime dateTime= DateTime.now();DateTime _pickedDate = // Some other DateTime instancedateTime.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');  }