Golang : Handle or parse date string with Z suffix(RFC3339) example
You want to convert a RFC3339 date string with Z suffix such as 2016-09-01T15:23:01Z
to Golang's time.Time
date type.
NOTE: The Z suffix in a given date string simply means UTC.
Below is an example on how to convert RFC3339 date string with Z suffix to time.Time
type. Example is taken and modify from the previous tutorial on converting date string to time.Time
variants.
Here you go!
package main
import (
"fmt"
"os"
"time"
)
func main() {
dateString := "2016-09-01T15:23:01Z"
fmt.Printf("Input : %s\n", dateString)
//convert date string of RFC3339 to time.Time type
// see RFC3339 in https://golang.org/pkg/time/#pkg-constants
// or if you prefer, you can see layOut as:
//layOut := "2006-01-02T15:04:05Z07:00" // yyyy-dd-MM
dateStamp, err := time.Parse(time.RFC3339, dateString)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("Output(local date) : %s\n", dateStamp.Local())
fmt.Printf("Output(UTC) : %s\n", dateStamp)
// format to E, MMM d yyyy
convertedDateString := dateStamp.Format("Mon 2-Jan-2006")
fmt.Printf("Output : %s\n", convertedDateString)
fmt.Printf("Full output : %s\n", dateStamp.Format("Monday 2-January-2006"))
location, err := time.LoadLocation("America/New_York")
if err != nil {
fmt.Println(err)
}
fmt.Println("Location : ", location, " Time : ", dateStamp.In(location))
location, _ = time.LoadLocation("Asia/Tokyo")
fmt.Println("Location : ", location, " Time : ", dateStamp.In(location))
}
Output:
Input : 2016-09-01T15:23:01Z
Output(local date) : 2016-09-01 23:23:01 +0800 SGT
Output(UTC) : 2016-09-01 15:23:01 +0000 UTC
Output : Thu 1-Sep-2016
Full output : Thursday 1-September-2016
Location : America/New_York Time : 2016-09-01 11:23:01 -0400 EDT
Location : Asia/Tokyo Time : 2016-09-02 00:23:01 +0900 JST
On the side note, even if the given date string does not have the Z
suffix - such as 2016-09-01T10:15:30+08:00
You still can parse the date string.
package main
import (
"fmt"
"os"
"time"
)
func main() {
dateString := "2016-09-01T10:15:30+08:00"
fmt.Printf("Input : %s\n", dateString)
//convert date string of RFC3339 to time.Time type
// see RFC3339 in https://golang.org/pkg/time/#pkg-constants
// or if you prefer, you can see layOut as:
//layOut := "2006-01-02T15:04:05Z07:00" // yyyy-dd-MM
dateStamp, err := time.Parse(time.RFC3339, dateString)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("Output(local date) : %s\n", dateStamp.Local())
fmt.Printf("Output(UTC) : %s\n", dateStamp)
// format to E, MMM d yyyy
convertedDateString := dateStamp.Format("Mon 2-Jan-2006")
fmt.Printf("Output : %s\n", convertedDateString)
fmt.Printf("Full output : %s\n", dateStamp.Format("Monday 2-January-2006"))
location, err := time.LoadLocation("America/New_York")
if err != nil {
fmt.Println(err)
}
fmt.Println("Location : ", location, " Time : ", dateStamp.In(location))
location, _ = time.LoadLocation("Asia/Tokyo")
fmt.Println("Location : ", location, " Time : ", dateStamp.In(location))
}
Input : 2016-09-01T10:15:30+08:00
Output(local date) : 2016-09-01 10:15:30 +0800 SGT
Output(UTC) : 2016-09-01 10:15:30 +0800 SGT
Output : Thu 1-Sep-2016
Full output : Thursday 1-September-2016
Location : America/New_York Time : 2016-08-31 22:15:30 -0400 EDT
Location : Asia/Tokyo Time : 2016-09-01 11:15:30 +0900 JST
References:
https://golang.org/pkg/time/#pkg-constants
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" // RFC822 with numeric zone
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" // RFC1123 with numeric zone
RFC3339 = "2006-01-02T15:04:05Z07:00"
RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
Kitchen = "3:04PM"
// Handy time stamps.
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"
See also : Golang : Convert date string to variants of time.Time type examples
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
+6.3k Golang : Experimental emojis or emoticons icons programming language
+4.4k Linux/MacOSX : How to symlink a file?
+6.4k Golang : Muxing with Martini example
+7.1k Golang : Dealing with struct's private part
+14k Golang : How to check if your program is running in a terminal
+18k Golang : Read binary file into memory
+9.6k Golang : Get escape characters \u form from unicode characters
+18.6k Golang : Calculate entire request body length during run time
+16.7k Golang : How to tell if a file is compressed either gzip or zip ?
+5.7k Golang : How to verify input is rune?
+6.8k Golang : Transform lisp or spinal case to Pascal case example