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

  • dateparse – parse date or time in unknown format. Can understand much formats, from the US and Chinese formats to UNIX timestamp.
  • when – a natural language date and time parser. Has rules for English and Russian.

Formatters

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

And bonus:

  • durafmt – format duration to string like “2 weeks 18 hours 22 minutes 3 seconds”.

Helpers

  • now – set of functions to calculate time based on another time. For example, now.New(t).BeginningOfMonth() returns first second of month in moment of t.
  • timeutil – contains timedelta like in Python and some operations with time.