1

I am working on an application in flutter where I have to get the difference between two dates in Years, Months and Dates

like below Ex.

final _date1 = DateTime(2019, 10, 15);
final _date2 = DateTime(2020, 12, 20);
Answer Difference = 1 Year, 2 Month, 5 Days

I searched a lot but unfortunately din't get a proper solution to calculate the difference between two dates in Years, Months including leap years.

Note: I know how to get the difference between two dates in Days i.e

 final _bd = DateTime(2020, 10, 12);
 final _date = DateTime.now();
 final _difference = _date.difference(_bd).inDays;
Anupam Mishra
  • 3,408
  • 5
  • 35
  • 63
  • This is duplicate of: https://stackoverflow.com/questions/62170347/flutter-how-to-find-difference-between-two-dates-in-years-months-and-days – Alex Radzishevsky Nov 29 '20 at 11:08
  • Does this answer your question? [Flutter: Finding Difference Between Two Dates](https://stackoverflow.com/questions/52713115/flutter-finding-difference-between-two-dates) – Payam Khaninejad Nov 29 '20 at 11:51
  • "2 months, 5 days" isn't a very meaningful measurement. "2 months" could mean anywhere between 59 and 62 days depending on when the endpoints are. – jamesdlin Nov 29 '20 at 12:16

3 Answers3

1

You may customize the following code as per your requirement to return a string or object or make it an extension:

void getDiffYMD(DateTime then, DateTime now) {
  int years = now.year - then.year;
  int months = now.month - then.month;
  int days = now.day - then.day;
  if (months < 0 || (months == 0 && days < 0)) {
    years--;
    months += (days < 0 ? 11 : 12);
  }
  if (days < 0) {
    final monthAgo = DateTime(now.year, now.month - 1, then.day);
    days = now.difference(monthAgo).inDays + 1;
  }

  print('$years years $months months $days days');
}
bluenile
  • 5,673
  • 3
  • 16
  • 29
0

The Answer might be a little long but will give you what you need.

This will give you the values as a String in the format "Year : year, Months: month, Days: days". You can change it according to your need.

You need to call differenceInYearsMonthsAndDays() and pass the startDate and endDate.

An Extension to calculate the differents in Months discarding the excess days.

extension DateTimeUtils on DateTime {
  int differenceInMonths(DateTime other) {
    if (isAfter(other)) {
      if (year > other.year) {
        if (day >= other.day) {
          return (12 + month) - other.month;
        } else {
          return (12 + month - 1) - other.month;
        }
      } else {
        if (day >= other.day) {
          return month - other.month;
        } else {
          return month - 1 - other.month;
        }
      }
    } else {
      return 0;
    }
  }
}

A Method to generate the Year, Month and Day Format

String differenceInYearsMonthsAndDays(
  DateTime startDate,
  DateTime endDate,
) {
  int days;
  final newStartDate = startDate.add(const Duration(days: -1));
  int months = endDate.differenceInMonths(newStartDate);
  if(months >= 12) {
    final years = months ~/ 12;
    final differenceInMonthsAndDays = differenceInMonthsAndDays(
          startDate.add(const Duration(year: years), 
          endDate,
    );
    return "Years : years, $differenceInMonthsAndDays";
  } else {
    return differenceInMonthsAndDays(
          startDate, 
          endDate,
    );
  }

}

A Method to generate the Month and Day Format

String differenceInMonthsAndDays(
  DateTime startDate,
  DateTime endDate,
) {
  int days;
  final newStartDate = startDate.add(const Duration(days: -1));
  int months = endDate.differenceInMonths(newStartDate);


  if (months > 0) {
    final tempDate = DateTime(
      newStartDate.year,
      newStartDate.month + months,
      newStartDate.day,
    );
    days = endDate.difference(tempDate).inDays;
    return days > 0 
       ? "Months : $months, Days : $days" 
       : "Months : $months";
  } else {
    days = endDate.difference(newStartDate).inDays;
    return "Days : $days";
  }
}
Jemsheer K D
  • 333
  • 3
  • 11
0
int _getYearDifference(DateTime from, DateTime to) {
    var diff = to.year - from.year;
    if (to.month < from.month || (to.month == from.month && to.day < from.day)) {
        diff--;
    }
    return diff;
}
Art
  • 1,302
  • 13
  • 25