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
+2.9k Java : Random alphabets, alpha-numeric or numbers only string generator
+4.7k Golang : Get S3 or CloudFront object or file information
+6.9k Golang Hello World Example
+16.2k Golang : Parse date string and convert to dd-mm-yyyy format
+14.2k Golang : How to login and logout with JWT example
+12.8k Golang : How to convert a number to words
+10.2k Golang : Format numbers to nearest thousands such as kilos millions billions and trillions
+4.9k Golang : Find change in a combination of coins example
+16.6k Golang : How to log each HTTP request to your web server?
+5k Golang : How to search a list of records or data structures
+4.9k Linux : Disable and enable IPv4 forwarding