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
+8.4k Golang : Quadratic example
+32.9k Golang : Integer is between a range
+6k Golang : Fibonacci number generator examples
+30.5k Golang : Copy directory - including sub-directories and files
+4.7k Linux/Unix/PHP : Restart PHP-FPM
+6.6k SSL : How to check if current certificate is sha1 or sha2 from command line
+5.9k Golang : Decode XML data from RSS feed
+5.1k AWS S3 : Prevent Hotlinking policy
+13.9k Golang : How to get Unix file descriptor for console and file
+10.7k Golang : How to detect a server/machine network interface capabilities?
+13.6k Golang : Accurate and reliable decimal calculations
+9.5k Golang : How to transmit update file to client by HTTP request example