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
+10.5k SSL : The certificate is not trusted because no issuer chain was provided
+4.4k Unix/Linux : How to archive and compress entire directory ?
+9.7k Golang : Simple image viewer with Go-GTK
+8.1k Golang : Create and shuffle deck of cards example
+8.3k Golang : Terminate-stay-resident or daemonize your program?
+6.7k Golang : How to execute code at certain day, hour and minute?
+12.1k Golang : Read from buffered reader until specific number of bytes
+7.7k Golang : Random integer with rand.Seed() within a given range
+9.1k Fix ERROR 1045 (28000): Access denied for user 'root'@'ip-address' (using password: YES)
+3.7k Linux : sudo yum updates not working
+12.7k Golang : Fix cannot use buffer (type bytes.Buffer) as type io.Writer(Write method has pointer receiver) error
+8.1k Golang : Play .WAV file from command line