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

前端 未结 6 1258
梦毁少年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 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.

提交回复
热议问题