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
+11.7k Golang : Pagination with go-paginator configuration example
+16.3k Golang : Get the IPv4 and IPv6 addresses for a specific network interface
+5.1k Swift : Convert string array to array example
+8.5k Golang : Find network service name from given port and protocol
+19.8k Golang : Count number of digits from given integer value
+22.1k Golang : Set and Get HTTP request headers example
+4.7k Python : Convert(cast) bytes to string example
+8.3k Golang : Convert(cast) []byte to io.Reader type
+33.4k Golang : convert(cast) bytes to string
+5.5k Golang : Find change in a combination of coins example
+11k Golang : Byte format example