How to Parse Time String in Go: Simple Guide with Examples
In Go, you parse a time string using
time.Parse(layout, value), where layout is a reference time format and value is the string to parse. The layout must match the format of the input string exactly to convert it into a time.Time object.Syntax
The basic syntax to parse a time string in Go is:
time.Parse(layout, value): Parses a formatted string and returns atime.Timeobject and an error.layout: A reference time format that defines the expected format of the input string. It uses the specific dateMon Jan 2 15:04:05 MST 2006as a model.value: The actual time string you want to convert.
go
t, err := time.Parse("2006-01-02 15:04:05", "2024-06-01 14:30:00") if err != nil { // handle error } // t is a time.Time object
Example
This example shows how to parse a date and time string into a time.Time object and print it.
go
package main import ( "fmt" "time" ) func main() { layout := "2006-01-02 15:04:05" timeStr := "2024-06-01 14:30:00" t, err := time.Parse(layout, timeStr) if err != nil { fmt.Println("Error parsing time:", err) return } fmt.Println("Parsed time:", t) }
Output
Parsed time: 2024-06-01 14:30:00 +0000 UTC
Common Pitfalls
Common mistakes when parsing time strings in Go include:
- Using a layout string that does not exactly match the input format.
- Confusing the layout format with common date format tokens; Go uses a specific reference date
Mon Jan 2 15:04:05 MST 2006to define layouts. - Ignoring the error returned by
time.Parse, which can cause silent failures.
Example of a wrong layout and the correct way:
go
package main import ( "fmt" "time" ) func main() { // Wrong layout: using common tokens instead of Go's reference time wrongLayout := "yyyy-mm-dd hh:mm:ss" timeStr := "2024-06-01 14:30:00" _, err := time.Parse(wrongLayout, timeStr) if err != nil { fmt.Println("Error with wrong layout:", err) } // Correct layout correctLayout := "2006-01-02 15:04:05" t, err := time.Parse(correctLayout, timeStr) if err != nil { fmt.Println("Error with correct layout:", err) return } fmt.Println("Parsed time correctly:", t) }
Output
Error with wrong layout: parsing time "2024-06-01 14:30:00" as "yyyy-mm-dd hh:mm:ss": cannot parse "2024-06-01 14:30:00" as "yyyy"
Parsed time correctly: 2024-06-01 14:30:00 +0000 UTC
Quick Reference
| Layout Token | Meaning | Example |
|---|---|---|
| 2006 | Year | 2024 |
| 01 | Month (zero-padded) | 06 |
| 02 | Day (zero-padded) | 01 |
| 15 | Hour (24-hour) | 14 |
| 04 | Minute | 30 |
| 05 | Second | 00 |
| MST | Time zone abbreviation | UTC |
Key Takeaways
Use time.Parse with a layout string matching Go's reference time format to parse time strings.
The layout must exactly match the input string format using Go's specific date tokens.
Always check the error returned by time.Parse to handle invalid formats.
Go's layout uses the date Mon Jan 2 15:04:05 MST 2006 as a reference, not common date format tokens.
Use the Quick Reference table to build correct layout strings for parsing.