Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: Find the number of days between two dates

I currently have a user's profile page that brings out their date of birth and other details. But I am planning to find the days before their birthday by calculating the difference between today's date and the date of birth obtained from the user.

User's Date of Birth

User DOB

And this is today's date obtained by using the intl package.

Today's date

I/flutter ( 5557): 09-10-2018 

The problem I am facing now is, How do I calculate the difference in days of these two dates?

Are there any specific formulas or packages that are available for me to check out?

like image 654
Asyraf Dayan Avatar asked Oct 09 '18 03:10

Asyraf Dayan


People also ask

How do you compare two dates in Flutter?

Simply use the methods isAfter() , isBefore() or isAtSameMomentAs() from DateTime . Other alternative, use compareTo(DateTime other) , as in the docs: Compares this DateTime object to [other], returning zero if the values are equal.

How do I calculate the number of days between two dates?

Using the DAYS Function Excel DAYS function can be used to calculate the total number of days when you have the start and the end date. You need to specify the 'Start Date' and the 'End Date' in the Days function, and it will give you the total number of days between the two specified dates.


2 Answers

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

like image 191
diegoveloper Avatar answered Sep 19 '22 20:09

diegoveloper


The accepted answer is wrong. Don't use it.

This is correct:

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

Testing:

DateTime date1 = DateTime.parse("2020-01-09 23:00:00.299871"); DateTime date2 = DateTime.parse("2020-01-10 00:00:00.299871");  expect(daysBetween(date1, date2), 1); // Works! 

Explanation why the accepted answer is wrong:

Just run this:

int daysBetween_wrong1(DateTime date1, DateTime date2) {   return date1.difference(date2).inDays; }  DateTime date1 = DateTime.parse("2020-01-09 23:00:00.299871"); DateTime date2 = DateTime.parse("2020-01-10 00:00:00.299871");  // Should return 1, but returns 0. expect(daysBetween_wrong1(date1, date2), 0); 

Note: Because of daylight savings, you can have a 23 hours difference between some day and the next day, even if you normalize to 0:00. That's why the following is ALSO incorrect:

// Fails, for example, when date2 was moved 1 hour before because of daylight savings. int daysBetween_wrong2(DateTime date1, DateTime date2) {   from = DateTime(date1.year, date1.month, date1.day);     to = DateTime(date2.year, date2.month, date2.day);   return date2.difference(date1).inDays;  } 

Rant: If you ask me, Dart DateTime is very bad. It should at least have basic stuff like daysBetween and also timezone treatment etc.


Update: The package https://pub.dev/packages/time_machine claims to be a port of Noda Time. If that's the case, and it's ported correctly (I haven't tested it yet) then that's the Date/Time package you should probably use.

like image 25
MarcG Avatar answered Sep 21 '22 20:09

MarcG