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
+5.7k Golang : How to validate ISBN?
+6.3k Golang : Gorrila mux.Vars() function example
+13.6k Golang : Normalize unicode strings for comparison purpose
+10.4k Golang : Fuzzy string search or approximate string matching example
+20.8k Golang : How to run Golang application such as web server in the background or as daemon?
+3.9k Golang : Experimental Jawi programming language
+5.1k PHP : How to check if an array is empty ?
+9.7k Golang : Replace a parameter's value inside a configuration file example
+6.1k Golang : How to detect if a sentence ends with a punctuation?
+3.6k Adding Skype actions such as call and chat into web page examples
+4.8k Golang : Markov chains to predict probability of next state example
+5.3k Golang & Javascript : How to save cropped image to file on server