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

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

提交回复
热议问题