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

前端 未结 6 1223
梦毁少年i
梦毁少年i 2021-01-16 12:48

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 J

相关标签:
6条回答
  • 2021-01-16 12:52

    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()
    
    0 讨论(0)
  • 2021-01-16 12:54

    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}");
    }
    
    0 讨论(0)
  • 2021-01-16 13:07
    DateTime a = DateTime(2015, 11, 29);
    DateTime b = DateTime(2007, 06, 27);
    int days = a.difference(b).inDays;
    
    0 讨论(0)
  • 2021-01-16 13:09

    the answer is yes, you can easilly achieve it with DateTime class in Dart. See: https://api.dart.dev/stable/2.8.3/dart-core/DateTime-class.html

    Example

    void main() {
      var moonLanding = DateTime(1969,07,20)
      var marsLanding = DateTime(2024,06,10);
      var diff = moonLanding.difference(marsLanding);
    
      print(diff.inDays.abs());
      print(diff.inMinutes.abs());
      print(diff.inHours.abs());
    }
    

    outputs: 20049 28870560 481176

    0 讨论(0)
  • 2021-01-16 13:12

    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);
    
    0 讨论(0)
  • 2021-01-16 13:14

    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.

    0 讨论(0)
提交回复
热议问题