Before I jump into explanation on how to parse string to time in Golang, let me show you a snippet of code of parsing string to time implementation in Golang…
layout := "2006-01-02 15:04:05"
value := "2023-12-14 17:09:47"
date, _ := time.Parse(layout, value)
fmt.Printf("date: %v\n", date)
… what do you think of this code?
It may confuse anyone who has any programming language background and start learning Golang.
Instead of using yyyy
for year layout, Golang uses 2006
. But don’t worry as I will explain to you how Golang string to time parsing works, and hope that you understand this feature when has finished reading this blog.
Golang Parsing Guide
Before going any further to explanation and example of string to time parsing in Golang, I would like you to read the following table as a guide for you.
Layout Format | Explanation | Example |
---|---|---|
2006 |
Year, 4 digits | 2023 |
006 |
Year, 3 digits | 023 |
06 |
Year, 2 digits | 23 |
01 |
Month, 2 digits | 07 |
1 |
Month, 1 digit (below month 10) | 7 |
January |
Month name, English letter | December |
Jan |
Month name, English letter 3 characters | Dec |
02 |
Day. 2 digits | 02 |
2 |
Day, 1 digit (below date 10) | 2 |
Monday |
Day name, English letter | Thursday |
Mon |
Day name, English letter 3 characters | Thu |
15 |
Hour, 24-hour format | 20 |
03 |
Hour, 12-hour format 2 digits | 08 |
3 |
Hour, 12-hour format 1 digit (below hour 10) | 8 |
PM |
AM/PM, usually used along with 12-hour format hour | AM |
04 |
Minute, 2 digits | 08 |
4 |
Minute, 1 digit (below minute 10) | 8 |
05 |
Second, 2 digits | 06 |
5 |
Second, 1 digit (below second 10) | 6 |
999999 |
Nano Second | 124006 |
MST |
Time Zone Location | EST |
Z0700 |
Time Zone Offset | -0200 |
Golang time.Parse Function
In order to parse string
to time.Time
in Golang, we can use time.Parse
function. This function requires 2 parameters, which are layout
and value
.
layout
is the layout format we want to use for the parsing process.
value
is the string value that we want to parse to time.
The following are some implementations of Golang parsing:
Implementation 1:
layout := "2006-01-02 15:04:05"
value := "2023-12-14 17:09:47"
date, _ := time.Parse(layout, value)
fmt.Printf("date: %v\n", date)
If you run the above snippet, you will get the following output:
Output 1:
date: 2023-12-14 17:09:47 +0000 UTC
Implementation 2:
layout := "02/01/2006 3.04.05 PM MST"
value := "14/12/2023 8.08.06 PM EST"
date, _ := time.Parse(layout, value)
fmt.Printf("date: %v\n", date)
If you run the above snippet, you will get the following output:
Output 2:
date: 2023-12-14 20:08:06 +0000 EST
More Examples
In this section, I will show you more example in parsing string to time in Golang. We will start from the easy and end it with more complex layout.
1. Parsing Year
Golang provides us with 3 options to represent year in the layout:
-
2006
represents 4 digits year
Implementation:
layout := "2006"
value := "2023"
date, _ := time.Parse(layout, value)
fmt.Printf("date: %v\n", date)
Output:
date: 2023-01-01 00:00:00 +0000 UTC
-
006
represents 3 digits year
Implementation:
layout := "006"
value := "023"
date, _ := time.Parse(layout, value)
fmt.Printf("date: %v\n", date)
Output:
date: 2023-01-01 00:00:00 +0000 UTC
-
06
represents 2 digits year
Implementation:
layout := "06"
value := "23"
date, _ := time.Parse(layout, value)
fmt.Printf("date: %v\n", date)
Output:
date: 2023-01-01 00:00:00 +0000 UTC
2. Parsing Month
Golang provides us with 2 options to represent month in the layout:
-
01
represents 2 digits month
Implementation:
layout := "01"
value := "08"
date, _ := time.Parse(layout, value)
fmt.Printf("date: %v\n", date)
Output:
date: 0000-08-01 00:00:00 +0000 UTC
-
1
represents 1 digit month
Implementation:
layout := "1"
value := "8"
date, _ := time.Parse(layout, value)
fmt.Printf("date: %v\n", date)
Output:
date: 0000-08-01 00:00:00 +0000 UTC
3. Parsing Month Name
Golang provides us with 2 options to represent month name in the layout:
-
January
represents full month name
Implementation:
layout := "January"
value := "December"
date, _ := time.Parse(layout, value)
fmt.Printf("date: %v\n", date)
Output:
date: 0000-12-01 00:00:00 +0000 UTC
-
Jan
represents 3 characters month name
Implementation:
layout := "Jan"
value := "Dec"
date, _ := time.Parse(layout, value)
fmt.Printf("date: %v\n", date)
Output:
date: 0000-12-01 00:00:00 +0000 UTC
4. Parsing Date
Golang provides us with 2 options to represent date in the layout:
-
02
represents 2 digits date
Implementation:
layout := "02"
value := "07"
date, _ := time.Parse(layout, value)
fmt.Printf("date: %v\n", date)
Output:
date: 0000-01-07 00:00:00 +0000 UTC
-
2
represents 1 digit date
Implementation:
layout := "2"
value := "7"
date, _ := time.Parse(layout, value)
fmt.Printf("date: %v\n", date)
Output:
date: 0000-01-07 00:00:00 +0000 UTC
5. Parsing Day Name
Golang provides us with 2 options to represent day name in the layout:
-
Monday
represents full day name
Implementation:
layout := "Monday"
value := "Tuesday"
date, _ := time.Parse(layout, value)
fmt.Printf("date: %v\n", date)
fmt.Printf("day name: %v\n", date.Weekday())
Output:
date: 0000-01-01 00:00:00 +0000 UTC
day name: Saturday
-
Mon
represents 3 characters day name
Implementation:
layout := "Mon"
value := "Fri"
date, _ := time.Parse(layout, value)
fmt.Printf("date: %v\n", date)
fmt.Printf("day name: %v\n", date.Weekday())
Output:
date: 0000-01-01 00:00:00 +0000 UTC
day name: Saturday
Note: in these two example, even tough you try to parse the day name. If you get the day name property of the date, you will get the actual day name, not the value you parsed.
6. Parsing Hour
Golang provides us with 2 options to represent hour in the layout:
-
15
represents 24-hour hour format
Implementation:
layout := "15"
value := "09"
date, _ := time.Parse(layout, value)
fmt.Printf("date: %v\n", date)
Output:
date: 0000-01-01 09:00:00 +0000 UTC
-
03
represents 2-digit 12-hour hour format
Implementation:
layout := "03"
value := "09"
date, _ := time.Parse(layout, value)
fmt.Printf("date: %v\n", date)
Output:
date: 0000-01-01 09:00:00 +0000 UTC
-
3
represents 1-digit 12-hour hour format
Implementation:
layout := "3"
value := "9"
date, _ := time.Parse(layout, value)
fmt.Printf("date: %v\n", date)
Output:
date: 0000-01-01 09:00:00 +0000 UTC
For the 12-hour format, you can provide AM/PM layout to give your value more clarity.
Implementation:
layout := "03 PM"
value := "07 AM"
date, _ := time.Parse(layout, value)
fmt.Printf("date: %v\n", date)
Output:
date: 0000-01-01 07:00:00 +0000 UTC
7. Parsing Minute
Golang provides us with 2 options to represent minute in the layout:
-
04
represents 2 digits minute
Implementation:
layout := "04"
value := "08"
date, _ := time.Parse(layout, value)
fmt.Printf("date: %v\n", date)
Output:
date: 0000-01-01 00:08:00 +0000 UTC
-
4
represents 1 digit minute
Implementation:
layout := "4"
value := "8"
date, _ := time.Parse(layout, value)
fmt.Printf("date: %v\n", date)
Output:
date: 0000-01-01 00:08:00 +0000 UTC
8. Parsing Second
Golang provides us with 2 options to represent second in the layout:
-
05
represents 2 digits second
Implementation:
layout := "05"
value := "03"
date, _ := time.Parse(layout, value)
fmt.Printf("date: %v\n", date)
Output:
date: 0000-01-01 00:00:03 +0000 UTC
-
5
represents 1 digit second
Implementation:
layout := "5"
value := "3"
date, _ := time.Parse(layout, value)
fmt.Printf("date: %v\n", date)
Output:
date: 0000-01-01 00:00:03 +0000 UTC
In addition, you can provide nanosecond layout for more detailed value.
Implementation:
layout := "5.999999"
value := "7.124006"
date, _ := time.Parse(layout, value)
fmt.Printf("date: %v\n", date)
fmt.Printf("nano second: %v\n", date.Nanosecond())
Output:
date: 0000-01-01 00:00:07.124006 +0000 UTC
nano second: 124006000
9. Parsing Timezone
MST
represents timezone location
Implementation:
layout := "MST"
value := "EST"
date, _ := time.Parse(layout, value)
fmt.Printf("date: %v\n", date)
Output:
date: 0000-01-01 00:00:00 +0000 EST
Z0700
represents timezone offset
Implementation:
layout := "Z0700"
value := "-0200"
date, _ := time.Parse(layout, value)
fmt.Printf("date: %v\n", date)
Output:
date: 0000-01-01 00:00:00 -0200 -0200
10. Complex Parsing Examples
Let’s say that I have value of 14/12/2023 22:15:27 EST
how can I parse this value into Golang time?
First of all, let’s define the layout of the value, which is 02/01/2006 15:04:05 MST
Implementation:
layout := "02/01/2006 15:04:05 MST"
value := "14/12/2023 22:15:27 EST"
date, _ := time.Parse(layout, value)
fmt.Printf("date: %v\n", date)
Output:
date: 2023-12-14 22:15:27 +0000 EST
There you go, I hope this blog can help you understand how to parse string to time in Golang. Thank you for reading, and have a nice day!
Source:
- https://dasarpemrogramangolang.novalagung.com/A-time-parsing-format.html
- https://pkg.go.dev/time#Parse
Cover image:
https://fastly.picsum.photos/id/626/1920/1080.jpg?hmac=4Br5Jm6QDs1qAaBlEvCubV6T687-IQqn2CCZkVWL4z4
Top comments (2)
thankyou this post is very helpful for me
Thank you, I'm glad my blog post helped you 😁