Golang Datetime Cheatsheet from strftime*
Tags: go, beginners, datetime
Published: 2021-07-25 12:22:03

---
title: Golang Datetime Cheatsheet from strftime*
published: true
date: 2021-07-25 11:31:31 UTC
tags: golang, beginner, datetime
canonical_url: https://alexandrubucur.com/tech/2021/golang-datetime-cheatsheet-from-strftime/
---

When I initially started working with Go the [time format ](https://pkg.go.dev/time#Parse) it seemed a bit clunky.

Maybe it's because it's a departure from the normal `strftime`, but also maybe it's also because there's no simple cheatsheet for the various date formatting options like you would see for example in the [PHP documentation](https://www.php.net/manual/en/datetime.format.php) or in the [Python documentation](https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes) or to use the fancy language of the day, the [Rust Chrono crate documentation](https://docs.rs/chrono/0.4.19/chrono/format/strftime/index.html#specifiers).

The alternative is to read the source code, specifically the [private constants](https://cs.opensource.google/go/go/+/refs/tags/go1.16.6:src/time/format.go;l=92).

That being said, here is a simple cheatsheet between the golang time format and php datetime format to make it easier to switch.
No two `strftime` implementations are identical on what they support across various languages (especially if you look at 0 prefixed minutes or seconds), but most of the basic things like `Y` for full year are present.


| `golang` character | `php` character     | Description                                                                                                                        | Example returned values                   |
|--------------------|---------------------|------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------|
| _Day_              | _Day_               | \---                                                                                                                               | \---                                      |
| `01`               | `d`                 | Day of the month, 2 digits with leading zeros                                                                                      | `01` to `31`                              |
| `Mon`              | `D`                 | A textual representation of a day, three letters                                                                                   | `Mon` through `Sun`                       |
| `1`                | `j`                 | Day of the month without leading zeros                                                                                             | `1` to `31`                               |
| `Monday`           | `l` (lowercase 'L') | A full textual representation of the day of the week                                                                               | `Sunday` through `Saturday`               |
| _Month_            | _Month_             | \---                                                                                                                               | \---                                      |
| `January`          | `F`                 | A full textual representation of a month, such as January or March                                                                 | `January` through `December`              |
| `01`               | `m`                 | Numeric representation of a month, with leading zeros                                                                              | `01` through `12`                         |
| `Jan`              | `M`                 | A short textual representation of a month, three letters                                                                           | `Jan` through `Dec`                       |
| `1`                | `n`                 | Numeric representation of a month, without leading zeros                                                                           | `1` through `12`                          |
| _Year_             | _Year_              | \---                                                                                                                               | \---                                      |
| `2006`             | `Y`                 | A full numeric representation of a year, 4 digits                                                                                  | Examples: `1999` or `2003`                |
| `06`               | `y`                 | A two digit representation of a year                                                                                               | Examples: `99` or `03`                    |
| _Time_             | _Time_              | \---                                                                                                                               | \---                                      |
| `pm`               | `a`                 | Lowercase Ante meridiem and Post meridiem                                                                                          | `am` or `pm`                              |
| `PM`               | `A`                 | Uppercase Ante meridiem and Post meridiem                                                                                          | `AM` or `PM`                              |
| `3`                | `g`                 | 12-hour format of an hour without leading zeros                                                                                    | `1` through `12`                          |
| `15`               | `G`                 | 24-hour format of an hour without leading zeros                                                                                    | `0` through `23`                          |
| `03`               | `h`                 | 12-hour format of an hour with leading zeros                                                                                       | `01` through `12`                         |
| `N/A`              | `H`                 | 24-hour format of an hour with leading zeros - not possible in Golang                                                              | `00` through `23`                         |
| `4`                | `i`                 | Minutes with leading zeros                                                                                                         | `00` to `59`                              |
| `04`               | `N/A`               | Minutes without leading zeros                                                                                                      | `0` to `59`                               |
| `5`                | `s`                 | Seconds with leading zeros                                                                                                         | `00` through `59`                         |
| `05`               | `N/A`               | Seconds without leading zeros                                                                                                      | `00` through `59`                         |
| _Timezone_         | _Timezone_          | \---                                                                                                                               | \---                                      |
| `MST`              | `e`                 | Timezone identifier                                                                                                                | Examples: `UTC`, `GMT`, `Atlantic/Azores` |
| `-0700`            | `O`                 | Difference to Greenwich time (GMT) without colon between hours and minutes                                                         | Example: `+0200`                          |
| `Z07:00`           | `p`                 | Difference to Greenwich time (GMT) with colon between hours and minutes                                                            | Example: `+02:00`                         |
| `MST`              | `T`                 | Timezone abbreviation                                                                                                              | Examples: `EST`, `MDT` ...                |
| `-070000`          | `Z`                 | Timezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive. | `-43200` through `50400`                  |