24
Convert Integer value to Hour:Minute:Seconds in dart
As a developer, there are times that you 'll need to convert a integer values to time remaining, It could be during the course of using Stream or something else.
Below is a good approach to handle the situation
Definitely to convert let's say 2 hours to seconds we 'll have 2*3600 = 7200 seconds
Definitely to convert 2minutes to seconds we 'll have
2*60 =120 seconds
String intToTimeLeft(int value) {
int h, m, s;
h = value ~/ 3600;
m = ((value - h * 3600)) ~/ 60;
s = value - (h * 3600) - (m * 60);
String result = "$h:$m:$s";
return result;
}
Output
0:0:0
String intToTimeLeft(int value) {
int h, m, s;
h = value ~/ 3600;
m = ((value - h * 3600)) ~/ 60;
s = value - (h * 3600) - (m * 60);
String hourLeft = h.toString().length < 2 ? "0" + h.toString() : h.toString();
String minuteLeft =
m.toString().length < 2 ? "0" + m.toString() : m.toString();
String secondsLeft =
s.toString().length < 2 ? "0" + s.toString() : s.toString();
String result = "$hourLeft:$minuteLeft:$secondsLeft";
return result;
}
Output
00:00:00
That wraps up everything ..See you in the next one
24