Time formatting and parsing
Context
Section titled “Context”Go uses a reference time Mon Jan 2 15:04:05 MST 2006 to format/parse. Use time.Format and time.Parse with this layout.
Example
Section titled “Example”Format a time and parse a string.
Code example
Section titled “Code example”package main
import ( "fmt" "time")
func main() { t := time.Date(2025, 3, 15, 14, 30, 0, 0, time.UTC) fmt.Println(t.Format("2006-01-02 15:04:05")) fmt.Println(t.Format("Monday, 02-Jan-06 03:04 PM"))
parsed, _ := time.Parse("2006-01-02", "2025-12-25") fmt.Println(parsed)}Output
Section titled “Output”2025-03-15 14:30:00Saturday, 15-Mar-25 02:30 PM2025-12-25 00:00:00 +0000 UTC