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
+6.1k Fix ERROR 2003 (HY000): Can't connect to MySQL server on 'IP address' (111)
+9.4k Golang : Detect Pascal, Kebab, Screaming Snake and Camel cases
+9.1k Golang : Intercept and compare HTTP response code example
+29.4k Golang : How to create new XML file ?
+10.6k Golang : Get local time and equivalent time in different time zone
+3.4k Golang : Fix go-cron set time not working issue
+14k Golang : Reverse IP address for reverse DNS lookup example
+25.5k Golang : convert rune to integer value
+19.2k Golang : Check if directory exist and create if does not exist
+30.5k Get client IP Address in Go
+25.7k Golang : How to write CSV data to file
+16.4k Golang : Check if a string contains multiple sub-strings in []string?