Golang program to convert milliseconds to minutes and seconds


In this golang article, we will write Go language programs to convert milliseconds to minutes and seconds using various methods. Milliseconds are converted to minutes and seconds mathematically using different set of logics.

Algorithm

  • Step 1 − Import the required packages in the program

  • Step 2 − Create a main function

  • Step 3 − In the main take milliseconds value and convert it into minutes and seconds using different logics mathematically

  • Step 4 − Then, print the output using Printf function from the fmt package

Example 1

In this Example, we will take the milliseconds value. Then, the seconds will be calculated by dividing the milliseconds by 1000 then we calculate minutes by dividing the seconds by 60.

package main

import (
   "fmt"
)

//Main function to execute the program
func main() {
   milliseconds_value := 234568
   seconds_value := milliseconds_value / 1000
   minutes_value := seconds_value / 60
   seconds_value = seconds_value % 60
   fmt.Println("The output is represented as:")
   fmt.Printf("%d milliseconds is %d minutes and %d seconds", milliseconds_value, minutes_value, seconds_value)
}

Output

The output is represented as:
234568 milliseconds is 3 minutes and 54 seconds

Example 2

In this Example, milliseconds will be taken as an input in milliseconds_value variable then firstly minutes will be calculated, dividing the milliseconds by 1000*60 then taking the modulo of entire output by 60.

package main

import (
   "fmt"
)

func main() {
   milliseconds_value := 246842
   minutes_value := (milliseconds_value / (1000 * 60)) % 60
   seconds_value := (milliseconds_value / 1000) % 60
   fmt.Println("The output is presented as:")
   fmt.Printf("%d milliseconds is %d minutes and %d seconds", milliseconds_value, minutes_value, seconds_value)
}

Output

The output is presented as:
246842 milliseconds is 4 minutes and 6 seconds

Example 3

In this illustration, the milliseconds will be obtained in the miliseconds_value variable. The minutes will be calculated- dividing the milliseconds by 1000*60, the seconds will be calculated- dividing the milliseconds by 1000 and then doing modulo of output by 60.

package main

import (
   "fmt"
)

func main() {
   milliseconds_value := 246842
   minutes_value := milliseconds_value / (1000 * 60)
   seconds_value := (milliseconds_value / 1000) % 60
   fmt.Printf("%d milliseconds is %d minutes and %d seconds", milliseconds_value, minutes_value, seconds_value)
}

Output

246842 milliseconds is 4 minutes and 6 seconds

Conclusion

We compiled and executed the program of converting the milliseconds to minutes and seconds using three Examples. In all the Examples we calculated minutes and seconds mathematically and applied different logics in each Example. All the Examples returned desired output.

Updated on: 03-May-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements