How to Find Difference Between Two Dates In Flutter?

· 3 min read
How to Find Difference Between Two Dates In Flutter
How to Find Difference Between Two Dates In Flutter

Sometimes the user needs to show the difference between two dates in a mobile application. So in this article, We will be discussing How to Find Difference Between Two Dates In Flutter?

How to Find Difference Between Two Dates In Flutter?

You can use the DateTime class to find the difference between the two years without using intl to format the date.

Code Snippet will look like below:

DateTime dob = DateTime.parse('1967-10-12');
Duration dur =  DateTime.now().difference(dob);
String differenceInYears = (dur.inDays/365).floor().toString();
return new Text(differenceInYears + ' years');

Users can use jiffy. Jiffy is a date dart package inspired by momentjs for parsing, manipulating, and formatting dates.

1. Relative Time

Jiffy("2011-10-31", "yyyy-MM-dd").fromNow(); // 8 years ago
Jiffy("2012-06-20").fromNow(); // 7 years ago

var jiffy1 = Jiffy()
    ..startOf(Units.DAY);
jiffy1.fromNow(); // 19 hours ago

var jiffy2 = Jiffy()
    ..endOf(Units.DAY);
jiffy2.fromNow(); // in 5 hours

var jiffy3 = Jiffy()
    ..startOf(Units.HOUR);
jiffy3.fromNow();

2. Date Manipulation:

var jiffy1 = Jiffy()
      ..add(duration: Duration(days: 1));
jiffy1.yMMMMd; // October 20, 2019

var jiffy2 = Jiffy()
    ..subtract(days: 1);
jiffy2.yMMMMd; // October 18, 2019

//  You can chain methods by using Dart method cascading
var jiffy3 = Jiffy()
     ..add(hours: 3, days: 1)
     ..subtract(minutes: 30, months: 1);
jiffy3.yMMMMEEEEdjm; // Friday, September 20, 2019 9:50 PM

var jiffy4 = Jiffy()
    ..add(duration: Duration(days: 1, hours: 3))
    ..subtract(duration: Duration(minutes: 30));
jiffy4.format("dd/MM/yyy"); // 20/10/2019


// Months and year are added in respect to how many 
// days there are in a months and if is a year is a leap year
Jiffy("2010/1/31", "yyyy-MM-dd"); // This is January 31
Jiffy([2010, 1, 31]).add(months: 1); /

The difference is measured in seconds and fractions of seconds. The difference above counts the number of fractional seconds between midnight at the beginning of those dates. If the dates above had been in local time, not UTC, then the difference between two midnights may not be a multiple of 24 hours due to daylight saving differences.

The returned Duration will be negative if the other occurs after this.

Kindly go through the difference method for further details.

Conclusion:

In this article, we have been through How to find the difference between two dates in flutter?

Keep Learning!!! Keep Fluttering !!!

Still, need a Support for Flutter Development?

FlutterAgency.com is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc.

FlutterAgency.com is one of the most popular online portal dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge on Flutter.

Leave a Reply