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
+4.6k PHP : Get coordinates latitude/longitude from string
+2.5k Golang : Measure execution time for a function
+5.2k Golang : Perform sanity checks on filename example
+3.7k Golang : Convert(cast) io.Reader type to string
+8.6k Golang : Basic authentication with .htpasswd file
+4.7k Golang : Get user input until a command or receive a word to stop
+7.4k Golang : How to get Unix file descriptor for console and file
+2.2k Golang : Calculate how many weeks left to go in a given year
+17.6k Golang : Calculate future date with time.Add() function
+8.4k Golang : Convert(cast) int to int64
+4.4k Golang : Check if user agent is a robot or crawler example
+4k Golang : Changing a RGBA image number of channels with OpenCV