PGH Web

PGH Web

Web Solutions from Pittsburgh

Go Datetime Format Builder

Build Go time.Format() strings interactively. Go's format system is based on a single reference time — Mon Jan 2 15:04:05 MST 2006 — where each component is a specific representation of that moment. Click tokens to compose your format string, see a live preview, and copy the result.

Build Your Format String

Examples

Year

Month

Weekday

Day

Hour

Minute

Second

AM/PM

Timezone

Subsecond

Separators (literals)

Preview

Output

Timezone name preview may differ from Go's output depending on the system timezone database.

Quick Reference

TokenCategoryExample outputDescription
2006Year20244-digit year
06Year242-digit year
JanuaryMonthMarchFull month name
JanMonthMarAbbreviated month name (3 chars)
01Month03Month number, zero-padded
1Month3Month number, no padding
MondayWeekdayTuesdayFull weekday name
MonWeekdayTueAbbreviated weekday name (3 chars)
02Day09Day of month, zero-padded
_2Day 9Day of month, space-padded
2Day9Day of month, no padding
15Hour14Hour (24-hour clock), zero-padded
03Hour02Hour (12-hour clock), zero-padded
3Hour2Hour (12-hour clock), no padding
04Minute05Minute, zero-padded
4Minute5Minute, no padding
05Second09Second, zero-padded
5Second9Second, no padding
PMAM/PMAMUppercase AM or PM
pmAM/PMamLowercase am or pm
MSTTimezoneESTTimezone abbreviation
-0700Timezone-0500UTC offset, no colon
-07:00Timezone-05:00UTC offset, with colon
-07Timezone-05UTC offset, hours only
Z0700TimezoneZ or -0500UTC offset; Z when UTC
Z07:00TimezoneZ or -05:00UTC offset with colon; Z when UTC
.000Subsecond.123Milliseconds, trailing zeros kept
.000000Subsecond.123456Microseconds, trailing zeros kept
.000000000Subsecond.123456789Nanoseconds, trailing zeros kept
.999Subsecond.123Milliseconds, trailing zeros removed
.999999Subsecond.123456Microseconds, trailing zeros removed
.999999999Subsecond.123456789Nanoseconds, trailing zeros removed

Frequently Asked Questions

Why does Go use numbers like 01, 02, 2006 in its format strings?

Go's time.Format() does not use placeholder tokens like %Y or YYYY. Instead, every format component is expressed as a specific representation of one reference moment: Mon Jan 2 15:04:05 MST 2006. That moment was chosen so that each component has a distinct value — month is 1, day is 2, hour is 15, minute is 4, second is 5, year is 2006. When you write 2006-01-02 in a Go format string, Go recognises 2006 as the year, 01 as the zero-padded month, and 02 as the zero-padded day because those are exactly the values they hold in the reference time.

What happens if I write a format string with tokens from another language, like %Y or YYYY?

Those characters are treated as literals. %Y produces the text %Y in the output, not the year. YYYY produces the text YYYY. This is one of the most common mistakes developers make when switching to Go from Python, PHP, or JavaScript. The reference time system means Go format strings look like a real date — 01/02/2006 is a valid Go format string because it happens to spell out the reference time's month, day, and year.

What is the difference between 2, 02, and _2 for the day of the month?

All three produce the day of the month but with different padding. 02 zero-pads single-digit days (1 becomes 01). _2 space-pads single-digit days (1 becomes  1, with a leading space). 2 applies no padding (1 stays 1). The same distinction applies to hours (3 vs 03), minutes (4 vs 04), seconds (5 vs 05), and months (1 vs 01). _2 is the only space-padded token.

How does Go handle timezone formatting?

Go provides six timezone offset tokens. -0700 emits a signed four-digit offset like -0500. -07:00 adds a colon: -05:00. -07 emits hours only: -05. Z0700 and Z07:00 behave like their dash counterparts but emit Z instead of +0000 when the time is in UTC. MST emits the timezone abbreviation. In this preview tool, timezone names come from the browser's Intl.DateTimeFormat API and may differ slightly from what Go reports on a particular server operating system.

What are the .000 and .999 subsecond tokens and when should I use each?

Both families format fractional seconds. .000, .000000, and .000000000 produce milliseconds, microseconds, and nanoseconds respectively, always including trailing zeros. .999, .999999, and .999999999 do the same but omit trailing zeros — if a timestamp has no fractional part, .999 emits nothing at all. Use the .000 family when you need a fixed-width output (e.g., log timestamps). Use the .999 family when you want compact output that omits insignificant zeros. In Go, these tokens must immediately follow a seconds token (05 or 5) in the format string; they are not standalone tokens.