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.3k Golang : How to stop user from directly running an executable file?
+4.4k Golang : Frobnicate or tweaking a string example
+32.6k Golang : convert(cast) bytes to string
+10k Golang : Concatenate (combine) buffer data example
+46.9k Golang : Upload file from web browser to server
+21k Golang : Repeat a character by multiple of x factor
+5.4k Golang : Convert an executable file into []byte example
+4.9k CodeIgniter/PHP : Remove empty lines above RSS or ATOM xml tag
+9.1k Android Studio : Checkbox for user to select options example
+5.6k Golang : Gargish-English language translator
+11.7k Golang : Convert(cast) uintptr to string example
+3.5k Javascript : How to show different content with noscript?