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 aString
representation of a date/time, parsing it withDateTime.parse
to get aDateTime
object, then usingDateFormat
to convert that back to aString
so that you can callDateTime.parse
on it again. Just useDateTime.parse
orDateFormat.parse
once.As Ben explained, you're using the wrong
DateFormat
pattern.MM
should be used for the month number;mm
should be used for minutes..ms
in yourDateFormat
pattern is also wrong; that means minutes and seconds, not milliseconds. You should useS
for fractional seconds. But since you're just parsing a date/time string in an ISO format, you don't needDateFormat
at all.-
var difference = now.difference(stored).inSeconds; Duration duration = Duration(seconds: difference);
This also doesn't make much sense.
now.difference(stored)
already returns aDuration
object. There's no point in converting aDuration
to a number of seconds back to another aDuration
unless 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.inMinutes
should return the minutes component of the duration, butinMinutes
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 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.