Formatting date and time in Golang

Feb 1, 2016 · 2 min read

There came a day when I needed to parse dates and do some date formatting in Golang. It’s easy to do it in PHP so how hard can it be in golang? My first idea was just pass in the same format as in PHP an see what I will get.

fmt.Println(time.Now().Format("Y-m-d H:i:s"))

The result was “Y-m-d H:i:s” and I was like “WTF is this?”.

OK it seems that placeholders for formatting date and time are different then in PHP. Next stop was documentation and there I find this

“Format returns a textual representation of the time value formatted according to layout, which defines the format by showing how the reference time, defined to be would be displayed if it were the value.”

Well that’s obvious that it should return formatted date as defined by the format, but where the hell are specification for these format placeholders?

Next I skim through all Time package documentation and search for something that would look like date format and I can’t find it. Strange. How come I can’t find anything which would look like date formats?

Next stop Google and search for date formatting in Golang tutorials. After reading couple of tutorials I finally figured out how date and time are formatted and why couldn’t find anything in documentation. That’s because date and time formats look like actual date and time and I just skipped all of them when I was looking at documentation.

So long story short. From Time package constants you can figure out how date and time are formatted but I still would prefer a detailed explanation for each placeholder and because of this reason generated a table with list of available placeholders for date and time formatting.

1609-09-12 19:02:35 PM +03:00 Sep Sat PDT 
--------------- + ------------ + ------------
Type | Placeholder | Value
--------------- + ------------ + ------------
Year | 2006 | 1609
Year | 06 | 09
Month | 01 | 09
Month | 1 | 9
Month | Jan | Sep
Month | January | September
Day | 02 | 12
Day | 2 | 12
Week day | Mon | Sat
Week day | Monday | Saturday
Hours | 03 | 07
Hours | 3 | 7
Hours | 15 | 19
Minutes | 04 | 02
Minutes | 4 | 2
Seconds | 05 | 35
Seconds | 5 | 35
AM or PM | PM | PM
Milliseconds | .000 | .123
Microseconds | .000000 | .123456
Nanoseconds | .000000000 | .123456789
Timezone offset | -0700 | +0300
Timezone offset | -07:00 | +03:00
Timezone offset | Z0700 | +0300
Timezone offset | Z07:00 | +03:00
Timezone | MST | PDT
--------------- + ------------ + ------------

Run the code in Go Playground with more dates.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store