Flutter - How to find difference between two dates in years, months and days?

5,553

Solution 1

I think it is not possible to do exactly what you want easily with DateTime. Therefore you can use https://pub.dev/packages/time_machine package that is quite powerful with date time handling:

import 'package:time_machine/time_machine.dart';

void main() {
  LocalDate a = LocalDate.today();
  LocalDate b = LocalDate.dateTime(DateTime(2022, 1, 2));
  Period diff = b.periodSince(a);
  print("years: ${diff.years}; months: ${diff.months}; days: ${diff.days}");
}

for hours/minutes/seconds precision:

import 'package:time_machine/time_machine.dart';

void main() {
  LocalDateTime a = LocalDateTime.now();
  LocalDateTime b = LocalDateTime.dateTime(DateTime(2022, 1, 2, 10, 15, 47));
  Period diff = b.periodSince(a);
  print("years: ${diff.years}; months: ${diff.months}; days: ${diff.days}; hours: ${diff.hours}; minutes: ${diff.minutes}; seconds: ${diff.seconds}");
}

Solution 2

What you are looking for is the Dart DateTime class You can get close to what you want in moment.js with

main() {
  var a = DateTime.utc(2015, 11, 29);
  var b = DateTime.utc(2007, 06, 27);

  var years = a.difference(b);
  print(years.inDays ~/365);

}

There is no inYears or inMonths option for DateTime though that's why the year is divided in the print. the difference function returns the difference in seconds so you have to process it yourself to days.

Solution 3

You can calculate from the total number of days:

void main() {
  DateTime a = DateTime(2015, 11, 29);
  DateTime b = DateTime(2007, 06, 27);
  int totalDays = a.difference(b).inDays;
  int years = totalDays ~/ 365;
  int months = (totalDays-years*365) ~/ 30;
  int days = totalDays-years*365-months*30;
  print("$years $months $days $totalDays");
}

Result is: 8 5 7 3077

Solution 4

You could write an extension on duration class to format it:

extension DurationExtensions on Duration {
  String toYearsMonthsDaysString() {
    final years = this.inDays ~/ 365
    // You will need a custom logic for the months part, since not every month has 30 days
    final months = (this.inDays ~% 365) ~/ 30
    final days = (this.inDays ~% 365) ~% 30

    return "$years years $months months $days days";
  }
}

The usage will be:

final date1 = DateTime()
final date2 = DateTime()
date1.difference(date2).toYearsMonthsDaysString()

Solution 5

You can use Jiffy Package for this like this

var jiffy1 = Jiffy("2008-10", "yyyy-MM");
var jiffy2 = Jiffy("2007-1", "yyyy-MM");

jiff1.diff(jiffy2, Units.YEAR); // 1
jiff1.diff(jiffy2, Units.YEAR, true);
Share:
5,553
Jainam Jhaveri
Author by

Jainam Jhaveri

learning mode on..

Updated on December 21, 2022

Comments

  • Jainam Jhaveri
    Jainam Jhaveri over 1 year

    I'm looking for a way to use DateTime to parse two dates, to show the difference. I want to have it on the format: "X years, Y months, Z days".

    For JS, we have momentjs library and following code::

    var a = moment([2015, 11, 29]);
    var b = moment([2007, 06, 27]);
    
    var years = a.diff(b, 'year');
    b.add(years, 'years');
    
    var months = a.diff(b, 'months');
    b.add(months, 'months');
    
    var days = a.diff(b, 'days');
    
    console.log(years + ' years ' + months + ' months ' + days + ' days');
    // 8 years 5 months 2 days
    

    Is there similar library available for dart that can help achieve this usecase?

  • Vilsad P P
    Vilsad P P about 4 years
    indays() will give the total duration in days, so is inhours() and in mitutes(). in order to get te result you need, you need to divide the inDays() with 365 (for years). like wise apply logic for months and days,
  • tjago
    tjago about 4 years
    yeah, agree here, it requires some additional computation to get years and months. Can be Alex answer using time_machine lib below is more suitable to get exact numbers. But for approximate data +- 1 day I would say multiplying days by 30 and years by 365 days could also serve, depending on the use case.
  • SRR
    SRR over 3 years
    How would you extend this to get hours minutes and seconds?
  • inaps
    inaps over 3 years
    Nice one! Thanks
  • Abdul Qadir
    Abdul Qadir almost 3 years
    Required difference in year, month and days. read question carefully
  • camillo777
    camillo777 almost 3 years
    @AbdulQadir from number of days you can easily calculate the other numbers; I added some code to show.