Flutter: Find the number of days between two dates

You can use the difference method provide by DateTime class

 //the birthday's date
 final birthday = DateTime(1967, 10, 12);
 final date2 = DateTime.now();
 final difference = date2.difference(birthday).inDays;

UPDATE

Since many of you reported there is a bug with this solution and to avoid more mistakes, I’ll add here the correct solution made by @MarcG, all the credits to him.

  int daysBetween(DateTime from, DateTime to) {
     from = DateTime(from.year, from.month, from.day);
     to = DateTime(to.year, to.month, to.day);
   return (to.difference(from).inHours / 24).round();
  }

   //the birthday's date
   final birthday = DateTime(1967, 10, 12);
   final date2 = DateTime.now();
   final difference = daysBetween(birthday, date2);

This is the original answer with full explanation: https://stackoverflow.com/a/67679455/666221

Leave a Comment