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:
DateTime.parse(DateFormat('yyyy-mm-dd hh:mm:ss.ms').format(DateTime.parse(date)));makes no sense. You're taking aStringrepresentation of a date/time, parsing it withDateTime.parseto get aDateTimeobject, then usingDateFormatto convert that back to aStringso that you can callDateTime.parseon it again. Just useDateTime.parseorDateFormat.parseonce.As Ben explained, you're using the wrong
DateFormatpattern.MMshould be used for the month number;mmshould be used for minutes..msin yourDateFormatpattern is also wrong; that means minutes and seconds, not milliseconds. You should useSfor fractional seconds. But since you're just parsing a date/time string in an ISO format, you don't needDateFormatat all.-
var difference = now.difference(stored).inSeconds; Duration duration = Duration(seconds: difference);This also doesn't make much sense.
now.difference(stored)already returns aDurationobject. There's no point in converting aDurationto a number of seconds back to another aDurationunless you're trying to explicitly discard any fractional seconds. -
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.inMinutesshould return the minutes component of the duration, butinMinutesreturns the total number of minutes. For example,Duration(hours: 1, minutes: 2).inMinuteswill return 62, not 2. If you instead want the minutes component, you will need to use something likeduration.inMinutes.remainder(60). Same thing applies forDuration.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.
