Common formats

Introduction

A date format is a pattern of tokens that tells a library how to write or read a date.

The same format has a different spelling in every language: %Y-%m-%d in strftime, YYYY-MM-DD in Moment, yyyy-MM-dd in Java.

The same date

ToolPatternResult
strftime%Y-%m-%d %H:%M2013-06-05 23:05
MomentYYYY-MM-DD HH:mm2013-06-05 23:05
PHPY-m-d H:i2013-06-05 23:05
Go2006-01-02 15:042013-06-05 23:05
Javayyyy-MM-dd HH:mm2013-06-05 23:05
C#yyyy-MM-dd HH:mm2013-06-05 23:05
Swiftyyyy-MM-dd HH:mm2013-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.

See: PostgreSQL formatting

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

ExampleOutput
%m/%d/%Y06/05/2013
%A, %B %e, %YWednesday, June 5, 2013
%F2013-06-05
%b %e %aJun 5 Wed

Time

ExampleOutput
%H:%M23:05
%I:%M %p11:05 PM
%T23:05:00

Used by Ruby, UNIX date, and many more.

See: strftime

Date

SymbolExampleArea
%aSunWeekday
%ASunday 
%w0..6 (Sunday is 0) 
%y13Year
%Y2013 
%bJanMonth
%BJanuary 
%m01..12 
%d01..31Day
%e1..31 

See: strftime

Time

SymbolExampleArea
%l1Hour
%H00..2324h hour
%I01..1212h hour
%M00..59Minute
%S00..60Second
%pAMAM or PM
%ZUTCZone name
%z+0000Numeric offset
%j001..366Day of the year
%s1370473500Epoch seconds
%%%Literal % character

See: strftime

Moment.js format

Examples

Date

ExampleOutput
YYYY-MM-DD2014-01-01
dddd, MMMM Do YYYYFriday, May 16th 2014
dddd [the] Do [of] MMMMFriday the 16th of May

Time

ExampleOutput
hh:mm a12:30 pm

Moment.js tokens; date-fns and Java SimpleDateFormat use different ones. Moment itself is in maintenance mode.

See: Format

Date

SymbolExampleArea
d0..6Weekday
ddSu 
dddSun 
ddddSunday 
YY13Year
YYYY2013 
M1..12 (Jan is 1)Month
Mo1st..12th 
MM01..12 (Jan is 1) 
MMMJan 
MMMMJanuary 
Q1..4Quarter
Qo1st..4th 
D1..31Day
Do1st..31st 
DD01..31 
DDD1..365Day of year
DDDo1st..365th 
DDDD001..365 
w1..53Week of year
wo1st..53rd 
ww01..53 

See: Format

Time

SymbolExampleArea
H0..2324h hour
HH00..23 
h1..1212h hour
hh01..12 
m0..59Minutes
mm00..59 
s0..59Seconds
ss00..59 
aamAM/PM
AAM 
Z+07:00Zone offset
ZZ+0730 
S0..9Deciseconds
SS00..99Centiseconds
SSS000..999Milliseconds
X1360013296Unix timestamp
x1360013296123Unix ms timestamp

See: Format

Presets

ExampleOutput
LT8:30 PM
LTS8:30:25 PM
LLAugust 2, 1985
llAug 2, 1985
LLLAugust 2, 1985 8:30 PM
lllAug 2, 1985 8:30 PM
LLLLFriday, August 2, 1985 8:30 PM
llllFri, Aug 2, 1985 8:30 PM

See: Long date formats

0 Comments for this cheatsheet. Write yours!