Golang : Convert seconds to minutes and remainder seconds
Problem:
You have an input seconds in integer that you want to convert into minutes and remainder seconds. How to do that?
Solution:
The secondsToMinutes()
function below will first get the minutes from the given input seconds and then calculate the remainder seconds.
Here you go!
package main
import (
"fmt"
)
func secondsToMinutes(inSeconds int) string {
minutes := inSeconds / 60
seconds := inSeconds % 60
str := fmt.Sprintf("d:d", minutes, seconds)
return str
}
func main() {
fmt.Println("3600 seconds in minutes : ", secondsToMinutes(3600))
fmt.Println("9999 seconds in minutes : ", secondsToMinutes(9999))
fmt.Println("660 seconds in minutes : ", secondsToMinutes(660))
fmt.Println("1234567890 seconds in minutes : ", secondsToMinutes(1234567890))
}
Output:
3600 seconds in minutes : 60:00
9999 seconds in minutes : 166:39
660 seconds in minutes : 11:00
1234567890 seconds in minutes : 20576131:30
See also : Golang : Convert date or time stamp from string to time.Time type
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+16k Golang : How to save log messages to file?
+23.9k Golang : Convert uint value to string type
+5.6k Unix/Linux : How to get own IP address ?
+3.7k MariaDB/MySQL : How to get version information
+11.2k Golang : Validate email address
+25.5k Golang : dial tcp: too many colons in address
+17.6k Golang : Get host name or domain name from IP address
+7.8k Golang : Write multiple lines or divide string into multiple lines
+29.2k Golang : How to redirect to new page with net/http?
+11.4k Golang : Get absolute path to binary for os.Exec function with exec.LookPath
+21.4k Golang : Test file read write permission example
+15k Golang : Get file permission