Parsing Date and Time in Go
Software Engineer with an entrepreneurship gene. Love talking about code, products, UI/UX and how to build startups.
Search for a command to run...
Software Engineer with an entrepreneurship gene. Love talking about code, products, UI/UX and how to build startups.
No comments yet. Be the first to comment.
Goroutines are one of the best features of Go language. It is very easy to run a function as a goroutine, you just have to use the keyword go before the function call. Here is a very simple function which sleeps for n seconds and prints a line. pa...

In a previous post, we saw how we can use Regular expressions in Go to match and replace patterns in a string. While regexps are very useful, you might not want to them for every time you want to do some basic string manipulation. The strings package...

Go is a modern language, but doesn't use try-except blocks to handle errors. Errors are simple values that can be returned from functions and it is common to check for errors before proceeding with your next steps. Let's see the different ways to cre...

In the previous post, we saw how to write a simple unit test case and also how to write table driven test cases. In this post we will quickly see how to write benchmarks, as go supports running benchmarks on your functions as part of the standard tes...
Go has built-in support for writing test cases for your code. And it is important to write as much test cases as possible to make sure you cover all possible conditions. You have to use the testing standard package to write your unit test cases. Let...
Pro Golang Dev
14 posts
Parsing a string containing date and time is one common task any programmer would encounter. It is also one of the most frustrating one, if you can't remember the specific format string correctly (is if %b or %B?).
Go does datetime parsing slightly differently. Instead of having cryptic format strings, go uses something called layouts. There is one special date that you need to remember whenever you need to parse dates in go.
Mon Jan 2 15:04:05 MST 2006
Why this specific date?
If you write it down in this format, it's obvious.
01/02 03:04:05PM ‘06 -0700.
It is January 2nd, 3:04:05 PM of 2006, UTC-0700.
Now whenever you need to parse a date string, you have to use this layout and pass to the time.Parse function.
And if you have a time.Time type, you can use the Format function to convert it to a string using the same layout structure.
input := "2018-04-24"
layout := "2006-01-02"
t, _ := time.Parse(layout, input)
fmt.Println(t) // 2018-04-24 00:00:00 +0000 UTC
fmt.Println(t.Format("02-Jan-2006")) // 24-Apr-2018
You can use this table below as a quick cheatsheet for the different date and time options and the layout params you need to use.
| Type | Options |
| Year | 06, 2006 |
| Month | 01, 1, Jan, January |
| Day | 02, 2, _2, (width two, right justified) |
| Weekday | Mon, Monday |
| Hours | 03, 3, 15 |
| Minutes | 04, 4 |
| Seconds | 05, 5 |
| ms μs ns | .000, .000000, .000000000 |
| ms μs ns | .999, .999999, .999999999 (trailing zeros removed) |
| am/pm | PM, pm |
| Timezone | MST |
| Offset | -0700, -07, -07:00, Z0700, Z07:00 |
| Common Formats | Layout |
| Date | January 2, 2006 |
| 01/02/06 | |
| Jan-02-06 | |
| Time | 15:04:05 |
| 3:04:05 PM | |
| Timestamp with microseconds | Jan _2 15:04:05 |
| Jan _2 15:04:05.000000 | |
| ISO 8601 | 2006-01-02T15:04:05-0700 |
| 2006-01-02 | |
| 15:04:05 | |
| RFC 822 with numeric zone | 02 Jan 06 15:04 MST |
| 02 Jan 06 15:04 -0700 | |
| RFC 1123 with numeric zone | Mon, 02 Jan 2006 15:04:05 MST |
| Mon, 02 Jan 2006 15:04:05 -0700 |