How to format time in Go/Golang
Tural Asgarov
Senior Software Engineer | Developer educator | Go | Golang | Node.js | DynamoDB
You can read original(with syntax highlighting) article on
Go uses a special "magic" reference time that might seem weird at first:
The Magic Reference Time is: 01/02 03:04:05PM 2006 MST
Or put another way: January 2, 2006 at 3:04:05 PM MST
Here's the genius part - the numbers in this date line up in order:
Let me show you with a super simple cheat sheet:
The trick to remember:
Some easy examples:
Pro Tips:
Think of it like a template - you're just showing Go how you want the date to look, using that special reference date as your model!
Now let's see how to handle timezones:
If we don't pay attention to the timezones, we can easily get wrong results.
Here are the GOLDEN RULES for server timezone handling:
IMPORTANT TIPS:
Official documentation for time in Go: https://pkg.go.dev/time
??