I want to get the remaining time between 22:00:00
and 00:22:00
in Flutter.
I've tried to find the difference but it's giving me 21:38:00
but what I actually want is 02:22:00
hrs remaining.
Asked
Active
Viewed 25 times
0

MendelG
- 14,885
- 4
- 25
- 52

Spicier Ewe
- 5
- 4
-
The problem is that with hh:mm:ss timestamps there is no way for the code to know that the second timestamp is referring to a time after midnight (ie in the next day) rather than in the morning of the same day. If you have access to the date as well as the time you could use that to compute an unambiguous result. – Jeremy Friesner Mar 03 '22 at 21:41
-
In the duplicate question, tooting my own, obviously biased horn, I recommend reading [my answer](https://stackoverflow.com/a/69712761/) and not reading just the top-voted/accepted answer. – jamesdlin Mar 03 '22 at 21:46
-
i get the times from an api it doesnt provide date but only the times – Spicier Ewe Mar 03 '22 at 21:47
-
With the time alone you won't be able to compute it correctly unless you add the date portion of it. For example, the code below gives you the correct output (if you run it on **DartPad.dev): ```flutter var firstDate = DateTime.parse('2022-03-03T22:00:00'); var secondDate = DateTime.parse('2022-03-04T00:22:00'); var diff = secondDate.difference(firstDate); print(diff); ``` You get **2:22:00.000000**, but is because you must deduct the first datetime from the current datetime (in our case, the second one you want). – Roman Jaquez Mar 03 '22 at 21:51