English Articles

How to work with date and time in Go

Standard library

Ok, there is how you can parse and format date or time string into time.Time object:

t = time.Parse(format, timeString)
t.Format(format)

And this format is the most strange thing in Go. There is an example of a format:

Mon, 02 Jan 2006 15:04:05 -0700

My first thought was “Wow, smart Go can get an example of a time string as format”. No. If you pass “2007” instead of “2006” your program will fail in runtime. It has to be the same values as in the example above.

In most cases, you don’t have to write these formats because Go has some constants for different time standards. For example, UnixDate, RFC822, RFC3339.

Parsers

Formatters

Let’s write 2006-01-2 with different formats.

And bonus:

Helpers

created: 2019-02-27 (Wed) updated: 2019-02-27 (Wed) views: 106

Contributors

orsinium