Golang : Get time.Duration in year, month, week or day
Problem:
When using time.Until()
or time.Since()
functions, the time.Duration type have methods such as Hour(), Minutes(), Nanoseconds() and Seconds() to return the duration in respective denomination. How to convert the duration into year, month, week or day?
Solution:
Use Seconds()
to retrieve the duration in seconds and then divide it with 86400
for days, 604800
for weeks and so on...
NOTE: This example is for converting time duration, not for calculating time differences. Duration != Difference
For calculating time differences, see https://socketloop.com/tutorials/golang-human-readable-time-elapsed-format-such-as-5-days-ago
Here you go!
package main
import (
"fmt"
"math"
"time"
)
func RoundTime(input float64) int {
var result float64
if input < 0 {
result = math.Ceil(input - 0.5)
} else {
result = math.Floor(input + 0.5)
}
// only interested in integer, ignore fractional
i, _ := math.Modf(result)
return int(i)
}
func main() {
layOut := "02/01/2006 15:04:05" // dd/mm/yyyy hh:mm:ss
future, err := time.Parse(layOut, "07/05/2018 15:12:10")
if err != nil {
panic(err)
}
diff := time.Until(future)
fmt.Println("Now : ", time.Now())
fmt.Println("Time reach the future date : ", future)
fmt.Println("Raw : ", diff)
fmt.Println("Hours : ", diff.Hours())
fmt.Println("Minutes : ", diff.Minutes())
fmt.Println("Seconds : ", diff.Seconds())
fmt.Println("Nano seconds : ", diff.Nanoseconds)
// get day, month and year
fmt.Println("Days : ", RoundTime(diff.Seconds()/86400))
fmt.Println("Weeks : ", RoundTime(diff.Seconds()/604800))
fmt.Println("Months : ", RoundTime(diff.Seconds()/2600640))
fmt.Println("Years : ", RoundTime(diff.Seconds()/31207680))
}
Sample output:
Now : 2017-05-06 21:13:09.450925287 +0800 MYT
Time reach the future date : 2018-05-07 15:12:10 +0000 UTC
Raw : 8785h59m0.54907493s
Hours : 8785.983485854147
Minutes : 527159.0091512488
Seconds : 3.162954054907493e+07
Nano seconds : 0x108d7b0
Days : 366
Weeks : 52
Months : 12
Years : 1
References:
https://golang.org/pkg/time/#Duration
https://socketloop.com/tutorials/golang-human-readable-time-elapsed-format-such-as-5-days-ago
See also : Golang : Human readable time elapsed format such as 5 days ago
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
+22.8k Golang : Round float to precision example
+10.9k Golang : How to transmit update file to client by HTTP request example
+5.3k PHP : See installed compiled-in-modules
+7.6k Golang : Dealing with struct's private part
+13.7k Golang : Qt progress dialog example
+5.3k Golang : PGX CopyFrom to insert rows into Postgres database
+7.4k Golang : Accessing dataframe-go element by row, column and name example
+5.9k Cash Flow : 50 days to pay your credit card debt
+14.8k Golang : Find commonalities in two slices or arrays example
+35.1k Golang : Upload and download file to/from AWS S3
+22.5k Golang : Convert Unix timestamp to UTC timestamp