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
+5.3k PHP : Proper way to get UTF-8 character or string length
+8.6k Golang : Validate IPv6 example
+22.4k Golang : Check if element exist in map
+16.5k Golang : Aligning strings to right, left and center with fill example
+7.3k Android Studio : Import third-party library or package into Gradle Scripts
+8.2k Golang : Scramble and unscramble text message by randomly replacing words
+13.8k Golang : How to check for empty array string or string?
+11.3k Golang : Sort and reverse sort a slice of bytes
+9.7k Golang : Create Temporary File
+18.2k Golang : Example for DSA(Digital Signature Algorithm) package functions
+12.3k Golang : Verify token from Google Authenticator App
+4.9k PHP : How to handle URI or URL with non-ASCII characters such as Chinese/Japanese/Korean(CJK) ?