Golang : Calculate future date with time.Add() function
Problem :
You need to calculate future dates by number of days. This can be use in calculating expiring dates or day to execute certain task. How to do that in Golang?
Solution :
Use time.Add()
function to calculate the different between today and future date.
For example :
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
fmt.Println("Today : ", now.Format("Mon, Jan 2, 2006 at 3:04pm"))
threeDays := time.Hour * 24 * 3
diff := now.Add(threeDays)
fmt.Println("Three days from now will be : ", diff.Format(time.ANSIC))
}
Sample output :
Today : Mon, May 18, 2015 at 11:03am
Three days from now will be : Thu May 21 11:03:31 2015
References :
http://golang.org/pkg/time/#Time.Add
https://www.socketloop.com/tutorials/golang-calculate-time-different
See also : Golang : Calculate time different
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.1k Golang : Concatenate (combine) buffer data example
+18.3k Golang : Measure http.Get() execution time
+4.9k PHP : Get client IP address
+9.5k Golang : Underscore string example
+5k Golang : Shuffle array of list
+18.6k Golang : Append content to a file
+18.4k Golang : Check if os.Stdin input data is piped or from terminal
+18.2k Golang : How to run your code only once with sync.Once object
+16.9k Golang : Display list of time zones with GMT
+7.2k Golang : Find relative luminance or color brightness
+12k Golang : List objects in AWS S3 bucket