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
+18.5k Golang : convert int to string
+13.3k Golang : Image to ASCII art example
+7.8k Findstr command the Grep equivalent for Windows
+12.9k Golang : Convert(cast) int to int64
+25.5k Golang : How to write CSV data to file
+13.7k Golang : Fix cannot use buffer (type bytes.Buffer) as type io.Writer(Write method has pointer receiver) error
+10.7k Golang : Fix go.exe is not compatible with the version of Windows you're running
+9.2k Golang : Find the length of big.Int variable example
+8k Golang : Check if integer is power of four example
+6.3k PHP : Shuffle to display different content or advertisement
+23.7k Golang : Upload to S3 with official aws-sdk-go package
+6.9k Golang : Use modern ciphers only in secure connection