Home > Back-end >  Find difference between two timestamps in Dart
Find difference between two timestamps in Dart

Time:12-06

I'm trying to find out the difference between two timestamps in Hours, Minutes, and Seconds and have managed to chalk out the below code to achieve the same. However, I don't seem to be getting the correct output. Can anyone please tell me where it is that I'm going wrong?

import 'package:intl/intl.dart';

void main() {
  String date = '2022-12-05 23:02:20';
  var stored =
      DateTime.parse(DateFormat('yyyy-mm-dd hh:mm:ss.ms').format(DateTime.parse(date)));
  var now = DateTime.now();
  
  var difference = now.difference(stored).inSeconds;
  
  Duration duration = Duration(seconds: difference);

  print('VALUE: $stored');            
  print('CURRENT TIME: $now');
  print(stored.runtimeType);
  print('HOURS: ${duration.inHours}');
  print('MINUTES: ${duration.inMinutes}');
  print('SECONDS: ${duration.inSeconds}');
}

This here is the output that I'm getting:

VALUE: 2022-02-05 11:02:20.220
CURRENT TIME: 2022-12-05 23:44:08.827
DateTime
HOURS: 7284
MINUTES: 437081
SECONDS: 26224908

Common mathematics suggests that the difference between 2022-12-05 23:44:08.827 and 2022-02-05 11:02:20.220 should produce 42 minutes and not 437081. Also, this was written on Dartpad

CodePudding user response:

You should be using MM instead of mm when parsing the date.

Fixed example:

import 'package:intl/intl.dart';

void main() {
  String date = '2022-12-05 23:02:20';
  var stored =
      DateTime.parse(DateFormat('yyyy-MM-dd hh:mm:ss.ms').format(DateTime.parse(date)));
  var now = DateTime.now();
  
  var difference = now.difference(stored).inSeconds;
  
  Duration duration = Duration(seconds: difference);

  print('VALUE: $stored');            
  print('CURRENT TIME: $now');
  print(stored.runtimeType);
  print('HOURS: ${duration.inHours}');
  print('MINUTES: ${duration.inMinutes}');
  print('SECONDS: ${duration.inSeconds}');
}

Output (13:29 EST timezone):

VALUE: 2022-12-05 11:02:20.220
CURRENT TIME: 2022-12-05 13:29:06.916
DateTime
HOURS: 2
MINUTES: 146
SECONDS: 8806

CodePudding user response:

There are a few things wrong:

  1. DateTime.parse(DateFormat('yyyy-mm-dd hh:mm:ss.ms').format(DateTime.parse(date))); makes no sense. You're taking a String representation of a date/time, parsing it with DateTime.parse to get a DateTime object, then using DateFormat to convert that back to a String so that you can call DateTime.parse on it again. Just use DateTime.parse or DateFormat.parse once.

  2. As Ben explained, you're using the wrong DateFormat pattern. MM should be used for the month number; mm should be used for minutes.

  3. .ms in your DateFormat pattern is also wrong; that means minutes and seconds, not milliseconds. You should use S for fractional seconds. But since you're just parsing a date/time string in an ISO format, you don't need DateFormat at all.

  4. var difference = now.difference(stored).inSeconds;
    
    Duration duration = Duration(seconds: difference);
    

    This also doesn't make much sense. now.difference(stored) already returns a Duration object. There's no point in converting a Duration to a number of seconds back to another a Duration unless you're trying to explicitly discard any fractional seconds.

  5. Common mathematics suggests that the difference between 2022-12-05 23:44:08.827 and 2022-02-05 11:02:20.220 should produce 42 minutes and not 437081.

    You seem to expect that Duration.inMinutes should return the minutes component of the duration, but inMinutes returns the total number of minutes. For example, Duration(hours: 1, minutes: 2).inMinutes will return 62, not 2. If you instead want the minutes component, you will need to use something like duration.inMinutes.remainder(60). Same thing applies for Duration.inSeconds.

Here is an adjusted version:

void main() {
  String date = '2022-12-05 23:02:20';
  var stored = DateTime.parse(date);
  var now = DateTime.now();
  
  var duration = now.difference(stored);

  print('VALUE: $stored');            
  print('CURRENT TIME: $now');
  print(stored.runtimeType);
  print('HOURS: ${duration.inHours}');
  print('MINUTES: ${duration.inMinutes.remainder(60)}');
  print('SECONDS: ${duration.inSeconds.remainder(60)}');
}

which for me outputs:

VALUE: 2022-12-05 23:02:20.000
CURRENT TIME: 2022-12-05 12:12:37.693
DateTime
HOURS: -10
MINUTES: -49
SECONDS: -42

Note that since the above code currently is subtracting a later time from an earlier time, the resulting difference is a negative Duration, so the output might look a little weird.

  •  Tags:  
  • dart
  • Related