Dart: Convert integer to hours and minutes

In this challenge, the task is to transform an integer into hours and minutes format, separated by colons. For instance, if the given number is 68, the corresponding output would be 1:8

Examples

Input: 128
Output: 2:8
Input: 35
Output: 0:35

Solution

This problem is straightforward. If we divide by 60, we get the number of hours. We can use the integer division ~/:

For example, the integer division 128 ~/ 60 result is 2 hours. And to get the minutes, we can use the modulo %. The result of the modulo is the remainder of the division, so 128 % 60 is 8 minutes.

It can be seen more clearly in the following image:

The result is 2:8

The result is 2:8

So, the final solution would look like this:

void main() {
  
  int num = 128;
  print('${(num ~/ 60)}:${num % 60}');
  
}

You can run the live example on DartPad:

Share this article