Skip to content

Instantly share code, notes, and snippets.

@code-shoily
Last active May 16, 2019 21:53
Show Gist options
  • Save code-shoily/f15b9a4ff1536561c2d2d221aeff1333 to your computer and use it in GitHub Desktop.
Save code-shoily/f15b9a4ff1536561c2d2d221aeff1333 to your computer and use it in GitHub Desktop.
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