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
+14.8k Golang : Normalize unicode strings for comparison purpose
+14.5k Golang : How to determine if user agent is a mobile device example
+7.1k Nginx : How to block user agent ?
+12.9k Golang : http.Get example
+26.6k Golang : Encrypt and decrypt data with AES crypto
+11.7k Golang : Convert(cast) float to int
+4.9k Javascript : How to get width and height of a div?
+7.4k Golang : Example of custom handler for Gorilla's Path usage.
+8k Golang : Sort words with first uppercase letter
+12.9k Python : Convert IPv6 address to decimal and back to IPv6
+12.3k Golang : Simple client-server HMAC authentication without SSL example
+5.1k Linux/Unix/MacOSX : Find out which application is listening to port 80 or use which IP version