Skip to content

Instantly share code, notes, and snippets.

@spatialtime
Created May 12, 2020 16:33
Show Gist options
  • Save spatialtime/7ccd52c0ae8a56fa46860d872792d77d to your computer and use it in GitHub Desktop.
Save spatialtime/7ccd52c0ae8a56fa46860d872792d77d to your computer and use it in GitHub Desktop.
Golang formatting and parsing of ISO 8601 dates, times, and time zones.
// This snippet demonstrates Golang formatting and parsing of
// ISO 8601 dates, times, time zones
import(
"fmt"
"time"
)
loc, _ := time.LoadLocation("America/Los_Angeles")
// Our example date/time:
// 2020-01-01T15:30:10.693-08:00
t := time.Date(2020, 1, 1, 15, 30, 10, 693000000, loc)
fmt.Println(t.Format("2006"))
// 2020
fmt.Println(t.Format("2006-01"))
// 2020-01
fmt.Println(t.Format("2006-01-02"))
// 2020-01-01
fmt.Println(t.Format("15:04"))
// 15:30
fmt.Println(t.Format("15:04:05"))
// 15:30:10
fmt.Println(t.Format("15:04:05.000"))
// 15:30:10.693
fmt.Println(t.Format("15:04:05.000-07"))
// 15:30:10.693-08
fmt.Println(t.Format("15:04:05.000-07:00"))
// 15:30:10.693-08:00
fmt.Println(t.Format(time.RFC3339))
// 2020-01-01T15:30:10-08:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment