Common formats
Introduction
The same date
| Tool | Pattern | Result |
|---|---|---|
| strftime | %Y-%m-%d %H:%M | 2013-06-05 23:05 |
| Moment | YYYY-MM-DD HH:mm | 2013-06-05 23:05 |
| PHP | Y-m-d H:i | 2013-06-05 23:05 |
| Go | 2006-01-02 15:04 | 2013-06-05 23:05 |
| Java | yyyy-MM-dd HH:mm | 2013-06-05 23:05 |
| C# | yyyy-MM-dd HH:mm | 2013-06-05 23:05 |
| Swift | yyyy-MM-dd HH:mm | 2013-06-05 23:05 |
JS Intl | { dateStyle: 'short' } | 05/06/2013 |
Each pattern writes 5 June 2013, 23:05 UTC. The Intl row is shaped by the
locale, so it can differ from machine to machine.
See: Date field symbols
ISO 8601
2013-06-05 # Date
2013-06-05T23:05:00Z # UTC
2013-06-05T23:05:00+02:00 # With an offset
2013-06-05T23:05:00.123Z # Milliseconds
ISO 8601 sorts lexicographically and needs no locale. Z means UTC.
See: ISO 8601
RFC 3339
2013-06-05T23:05:00Z
2013-06-05T23:05:00.123+02:00
2013-06-05 23:05:00Z # Space instead of T
RFC 3339 is the internet profile of ISO 8601, and what toISOString()
emits. Most parsers accept the space, but not all of them.
See: RFC 3339
RFC 2822
Wed, 05 Jun 2013 23:05:00 +0000
05 Jun 2013 23:05:00 GMT
Email headers use this format. RFC 5322 obsoletes RFC 2822 with the same date syntax.
See: RFC 5322
Unix time
date +%s # 1370473500
date -d @1370473500 # GNU date: Wed Jun 5 2013
Seconds since 1970-01-01T00:00:00Z, ignoring leap seconds. JavaScript
and Java's Date count milliseconds.
See: date(1)
SQL
SELECT DATE_FORMAT(NOW(), '%Y-%m-%d'); -- MySQL
SELECT to_char(NOW(), 'YYYY-MM-DD'); -- PostgreSQL
SELECT strftime('%Y-%m-%d', 'now'); -- SQLite
SELECT FORMAT(GETDATE(), 'yyyy-MM-dd'); -- SQL Server
MySQL and SQLite copy strftime tokens; PostgreSQL and SQL Server use their own patterns.
Parse vs format
format Date -> String # tokens: %Y-%m-%d
parse String -> Date # the same pattern, or a parser
Parsing and formatting share a pattern in strftime, Moment, Go, and Java,
but not everywhere: JavaScript's Date reliably parses only ISO 8601, and
PHP's DateTime::createFromFormat takes its own token set.
See: Date.parse
Language reference
JavaScript
new Intl.DateTimeFormat('en-GB', {
dateStyle: 'long',
timeStyle: 'short',
timeZone: 'UTC'
}).format(date) // 5 June 2013 at 23:05
date.toISOString() // 2013-06-05T23:05:00.000Z
Intl is for humans and follows the locale; toISOString() is always
UTC and always ISO 8601.
See: Intl.DateTimeFormat
PHP
date('Y-m-d H:i:s'); // 2013-06-05 23:05:00
date('D, d M Y', $timestamp); // Wed, 05 Jun 2013
PHP has its own tokens. strftime() was deprecated in PHP 8.1, so use
date() or DateTime::format().
See: PHP date()
Go
t.Format("2006-01-02 15:04:05")
t.Format(time.RFC3339)
t.Format("Mon, 02 Jan 2006")
A Go layout is a sample date, not a pattern: Mon Jan 2 15:04:05 MST 2006.
Change the sample to change the output.
See: time.Format
Java
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(now);
DateTimeFormatter.ISO_LOCAL_DATE.format(now);
DateTimeFormatter.RFC_1123_DATE_TIME.format(now);
yyyy is the calendar year; YYYY is the week-based year, which differs
near New Year.
See: DateTimeFormatter
Swift
let f = DateFormatter()
f.dateFormat = "yyyy-MM-dd HH:mm"
f.string(from: date)
ISO8601DateFormatter().string(from: date)
DateFormatter follows Unicode TR35 patterns. ISO8601DateFormatter is
the safe way to write ISO 8601.
See: DateFormatter
C#
date.ToString("yyyy-MM-dd HH:mm:ss")
date.ToString("O") // Round-trip ISO 8601
date.ToString("R") // RFC 1123
Custom patterns are case-sensitive: MM is the month, mm is minutes.
See: Custom date formats
strftime format
Presets
Date
| Example | Output |
|---|---|
%m/%d/%Y | 06/05/2013 |
%A, %B %e, %Y | Wednesday, June 5, 2013 |
%F | 2013-06-05 |
%b %e %a | Jun 5 Wed |
Time
| Example | Output |
|---|---|
%H:%M | 23:05 |
%I:%M %p | 11:05 PM |
%T | 23:05:00 |
Used by Ruby, UNIX date, and many more.
See: strftime
Date
| Symbol | Example | Area |
|---|---|---|
%a | Sun | Weekday |
%A | Sunday | |
%w | 0..6 (Sunday is 0) | |
%y | 13 | Year |
%Y | 2013 | |
%b | Jan | Month |
%B | January | |
%m | 01..12 | |
%d | 01..31 | Day |
%e | 1..31 |
See: strftime
Time
| Symbol | Example | Area |
|---|---|---|
%l | 1 | Hour |
%H | 00..23 | 24h hour |
%I | 01..12 | 12h hour |
%M | 00..59 | Minute |
%S | 00..60 | Second |
%p | AM | AM or PM |
%Z | UTC | Zone name |
%z | +0000 | Numeric offset |
%j | 001..366 | Day of the year |
%s | 1370473500 | Epoch seconds |
%% | % | Literal % character |
See: strftime
Moment.js format
Examples
Date
| Example | Output |
|---|---|
YYYY-MM-DD | 2014-01-01 |
dddd, MMMM Do YYYY | Friday, May 16th 2014 |
dddd [the] Do [of] MMMM | Friday the 16th of May |
Time
| Example | Output |
|---|---|
hh:mm a | 12:30 pm |
Moment.js tokens; date-fns and Java SimpleDateFormat use different ones. Moment itself is in maintenance mode.
See: Format
Date
| Symbol | Example | Area |
|---|---|---|
d | 0..6 | Weekday |
dd | Su | |
ddd | Sun | |
dddd | Sunday | |
YY | 13 | Year |
YYYY | 2013 | |
M | 1..12 (Jan is 1) | Month |
Mo | 1st..12th | |
MM | 01..12 (Jan is 1) | |
MMM | Jan | |
MMMM | January | |
Q | 1..4 | Quarter |
Qo | 1st..4th | |
D | 1..31 | Day |
Do | 1st..31st | |
DD | 01..31 | |
DDD | 1..365 | Day of year |
DDDo | 1st..365th | |
DDDD | 001..365 | |
w | 1..53 | Week of year |
wo | 1st..53rd | |
ww | 01..53 |
See: Format
Time
| Symbol | Example | Area |
|---|---|---|
H | 0..23 | 24h hour |
HH | 00..23 | |
h | 1..12 | 12h hour |
hh | 01..12 | |
m | 0..59 | Minutes |
mm | 00..59 | |
s | 0..59 | Seconds |
ss | 00..59 | |
a | am | AM/PM |
A | AM | |
Z | +07:00 | Zone offset |
ZZ | +0730 | |
S | 0..9 | Deciseconds |
SS | 00..99 | Centiseconds |
SSS | 000..999 | Milliseconds |
X | 1360013296 | Unix timestamp |
x | 1360013296123 | Unix ms timestamp |
See: Format
Presets
| Example | Output |
|---|---|
LT | 8:30 PM |
LTS | 8:30:25 PM |
LL | August 2, 1985 |
ll | Aug 2, 1985 |
LLL | August 2, 1985 8:30 PM |
lll | Aug 2, 1985 8:30 PM |
LLLL | Friday, August 2, 1985 8:30 PM |
llll | Fri, Aug 2, 1985 8:30 PM |
See: Long date formats
0 Comments for this cheatsheet. Write yours!