Home > Mobile >  Dart, get difference between two DateTimes in (Days-Hours-Minutes)?
Dart, get difference between two DateTimes in (Days-Hours-Minutes)?

Time:04-02

How to get difference between two DateTimes in (Days-Hours-Minutes)?

        DateTime a = someTimeStamp.toDate();
        print(a);
        final now = DateTime.now();
        print(now);
        var difference = now.difference(a).inMilliseconds;
        print(difference);
        var dt = DateTime.fromMillisecondsSinceEpoch(difference);
        print(dt);

it shows 1970-01-05 06:40:01.632 at the end

CodePudding user response:

You can day difference between two dates like this, or use difference.inHours to get the difference in hours.

 final date1 = someTimeStamp.toDate();
 final date2 = DateTime.now();
 final difference = date2.difference(date1).inDays;

CodePudding user response:

  var nowTime = DateTime.now();
  var diff = nowTime.difference(pretime);
  print(diff.inDays.toString()  
      'd:'  
      diff.inHours.toString()  
      'h:'  
      diff.inMinutes.toString()  
      'm:'  
      diff.inSeconds.toString()  
      's');

OutPut: enter image description here

  • Related