Last active
May 16, 2019 21:53
-
-
Save code-shoily/f15b9a4ff1536561c2d2d221aeff1333 to your computer and use it in GitHub Desktop.
Reply for /r/dartlang - https://www.reddit.com/r/dartlang/comments/bpfszt/return_length_of_time_between_two_strings_of_hhmm/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Duration toDuration(String duration) { | |
var tokens = duration.split(":").map(int.parse).toList(); | |
if (tokens.length == 3) | |
return Duration(hours: tokens[0], minutes: tokens[1], seconds: tokens[2]); | |
else | |
return Duration(hours: tokens[0], minutes: tokens[1]); | |
} | |
Duration timediff(String startTime, String endTime) { | |
var startTimeDuration = toDuration(startTime); | |
var endTimeDuration = toDuration(endTime); | |
if (endTimeDuration.compareTo(startTimeDuration) >= 0) | |
return endTimeDuration - startTimeDuration; | |
else | |
return (endTimeDuration + Duration(hours: 24)) - startTimeDuration; | |
} | |
main() { | |
print(timediff("15:12", "16:21")); | |
print(timediff("13:55", "23:12")); | |
print(timediff("21:15", "13:09")); | |
print(timediff("16:55", "00:11")); | |
print(timediff("21:16", "14:41")); | |
print(timediff("12:55", "15:02")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment