In this tutorial, I will let you know some basic formats of date time using golang time package.The Go provides the time package which have some handy method for commonly used date formats:
The current time can be determined using time.Now(), provided by the time package.
/* main.go */
package main
import ("time"
"fmt");
func main() {
t := time.Now()
fmt.Printf("today date is = %s", t)
}
The above code will return current system datetime.
Output :
today date is = 2020-02-25 12:32:26.271167524 +0000 UTC m=+0.000332306
UTC Time in Golang
You can also get date format in UTC by appending .UTC()
/* main.go */
package main
import ("time"
"fmt");
func main() {
t := time.Now().UTC()
fmt.Printf("today date is = %s", t)
}
Output :
today date is = 2020-02-25 12:35:30.711465968 +0000 UTC
How To Get Unix Timestamp in Golang
The go time package also provide method that convert current datetime into unix timestamp –
/* main.go */
package main
import ("time"
"fmt");
func main() {
t := time.Now().Unix()
fmt.Printf("today date in UNIX = %d", t)
}
Output is –
today date in UNIX = 1582634239
How To Print Datetime in Custom Format
You can also format the datetime as per your need, You just need to pass your date format into the time.Format() method –
package main
import ("time"
"fmt");
func main() {
t := time.Now()
fmt.Printf("today date = %s", t)
fmt.Printf("\ntoday date in format = %s", t.Format("Mon Jan 2 15:04:05 -0700 MST 2006"))
}
Output –
today date = 2020-02-26 12:11:38.276938191 +0000 UTC m=+0.000237004 today date in format = Wed Feb 26 12:11:38 +0000 UTC 2020
How To get Yesterday Date in Golang
We can generate yesterday date or past date using golang AddDate() method –
package main
import ("time"
"fmt");
func main() {
t := time.Now()
fmt.Println("today date = %s", t)
fmt.Println("yesterday date = %s", t.AddDate(0, 0, -1))
fmt.Println("last month date = %s", t.AddDate(0, -1, 0))
fmt.Println("last year date = %s", t.AddDate(-1, 0, 0))
}
Output –
today date = %s 2020-02-26 12:13:14.900923371 +0000 UTC m=+0.000212148 yesterday date = %s 2020-02-25 12:13:14.900923371 +0000 UTC last month date = %s 2020-01-26 12:13:14.900923371 +0000 UTC last year date = %s 2019-02-26 12:13:14.900923371 +0000 UTC
How To get Tomorrow Date in Golang
We can generate tomorrow date or future date using golang AddDate() method –
package main
import ("time"
"fmt");
func main() {
t := time.Now()
fmt.Println("today date = %s", t)
fmt.Println("tomorrow date = %s", t.AddDate(0, 0, 1))
fmt.Println("next month date = %s", t.AddDate(0, 1, 0))
fmt.Println("next year date = %s", t.AddDate(1, 0, 0))
}
Output –
today date = %s 2020-02-26 12:16:53.961601321 +0000 UTC m=+0.000221758 tomorrow date = %s 2020-02-27 12:16:53.961601321 +0000 UTC next month date = %s 2020-03-26 12:16:53.961601321 +0000 UTC next year date = %s 2021-02-26 12:16:53.961601321 +0000 UTC
The following predefined date and timestamp format constants are also available –
- ANSIC = “Mon Jan _2 15:04:05 2006”
- UnixDate = “Mon Jan _2 15:04:05 MST 2006”
- RubyDate = “Mon Jan 02 15:04:05 -0700 2006”
- RFC822 = “02 Jan 06 15:04 MST”
- RFC822Z = “02 Jan 06 15:04 -0700”
- RFC850 = “Monday, 02-Jan-06 15:04:05 MST”
- RFC1123 = “Mon, 02 Jan 2006 15:04:05 MST”
- RFC1123Z = “Mon, 02 Jan 2006 15:04:05 -0700”
- RFC3339 = “2006-01-02T15:04:05Z07:00”
- RFC3339Nano = “2006-01-02T15:04:05.999999999Z07:00”
- Kitchen = “3:04PM”
- Stamp = “Jan _2 15:04:05”
- StampMilli = “Jan _2 15:04:05.000”
- StampMicro = “Jan _2 15:04:05.000000”
- StampNano = “Jan _2 15:04:05.000000000”
