MiServe DevTools

Your lid

Pin tools here. They are saved in this browser.

Go Time Format & strftime Converter

Write a Go time layout and see the current time formatted with it, convert between Go layouts and strftime, and look up the reference-time tokens.

Edit either field and the other one follows.

The current time in this layout

Named layouts
Reference-time tokens

Go's reference time is Mon Jan 2 15:04:05 MST 2006. Written as 01/02 03:04:05PM '06 -0700, its parts count from 1 to 7: month, day, hour, minute, second, year and zone offset. Select a token to insert it into the layout at the cursor.

Go tokenMeaningstrftimeNow
Year, four digits %Y
Year, last two digits %y
Month as a number, two digits %m
Month as a number, without a leading zero %-m
Month name, abbreviated %b
Month name, in full %B
Day of the month, two digits %d
Day of the month, without a leading zero %-d
Day of the month, padded with a space %e
Day of the year, three digits %j
Hour on the 24-hour clock, two digits %H
Hour on the 12-hour clock, two digits %I
Hour on the 12-hour clock, without a leading zero %-I
Minute, two digits %M
Second, two digits %S
AM or PM %p
Weekday name, abbreviated %a
Weekday name, in full %A
Time zone abbreviation %Z
UTC offset in hours and minutes, without a colon %z
UTC offset in hours and minutes, with a colon %:z
UTC offset with a colon, or the letter Z when the offset is zero —
Fraction of a second with a fixed number of digits, one for each zero .%L
Fraction of a second without trailing zeros, at most one digit for each nine —

Runs in your browser with WebAssembly. Nothing you paste is uploaded.

About this tool

A layout is an example, not a pattern

Most languages describe a date format with placeholders such as %Y-%m-%d or YYYY-MM-DD. Go does it differently: you write out how one particular moment, the reference time, should look, and time.Format prints any other time the same way. The reference time is Mon Jan 2 15:04:05 MST 2006.

The values were chosen so that they count upwards when written in the American order 01/02 03:04:05PM '06 -0700: month 1, day 2, hour 3 (or 15 on the 24-hour clock), minute 4, second 5, year 6 and a zone offset of minus 7 hours. Each value can only stand for one thing, which is how Go knows that 01 is a month and 04 a minute.

Common mistakes

Because 01 is the month and 02 the day, a day-first date is 02/01/2006, which looks wrong to anyone used to the reference order. A layout written with other habits does not fail either. Go prints what it does not recognise as literal text, so YYYY-MM-DD comes out unchanged, and it reads every digit it does recognise as a token, so 2021-12-25 comes out as unrelated numbers.

There is no way to escape a token. Text that happens to contain one is replaced, so a literal Jan, Mon or a digit such as 2 in a layout turns into a value. Add such text outside the layout instead.

Z07:00 prints Z for UTC and a numeric offset otherwise, which is what RFC 3339 expects, while -07:00 always prints the number. For fractions of a second, .000 keeps trailing zeros and .999 removes them. Month and weekday names are always in English.

How the preview and the conversion work

The preview is produced by Go's own time.Format, compiled to WebAssembly and run in your browser, so it shows what a Go program prints for the same moment and offset. Your browser supplies the current time, the UTC offset of your time zone and its abbreviation. When the browser has no abbreviation for your zone, a numeric one such as +03 is used, which is also what Go prints for zones that have no letters of their own. For some zones the browser's abbreviation differs from the one in the time zone database that Go reads.

The two formats do not cover the same ground. strftime has no directive that behaves like Z07:00, and Go has no token for week numbers (%U, %W, %V), for the weekday as a number (%u, %w) or for seconds since the epoch (%s). The converter copies such parts unchanged and names them below the field. It also names literal text in a strftime format that Go would read as a token, because a layout cannot hold it.

Some directives on the strftime side are extensions that not every implementation has: the flag that removes padding (%-d), the offset with a colon (%:z) and the fraction directives %L, %f and %N. Check the documentation of the language you are converting for.

Questions

Why does Go use 2006-01-02 15:04:05 instead of YYYY-MM-DD?

Go layouts show how a fixed reference time, Mon Jan 2 15:04:05 MST 2006, should be printed. Its parts are the numbers 1 to 7 in the order month, day, hour, minute, second, year, zone offset, so every number identifies one part of the date without any placeholder syntax.

What is the Go layout for an ISO 8601 or RFC 3339 timestamp?

Use 2006-01-02T15:04:05Z07:00, which is the constant time.RFC3339. For fractions of a second use time.RFC3339Nano, which is 2006-01-02T15:04:05.999999999Z07:00.

How do I print milliseconds in a Go layout?

Add .000 after the seconds, as in 15:04:05.000. Each zero is one digit, so .000000 gives microseconds. Use nines instead, as in .999, to drop trailing zeros.

How do I get a 24-hour clock in a Go layout?

Write the hour as 15. The values 03 and 3 mean the 12-hour clock and are normally combined with PM, which prints AM or PM.

More in Time