Date, Time & DateTime Formatting Cheatsheet

Copy Markdown View Source

Date formatting

Localize.Date.to_string(~D[2025-07-10])
#=> {:ok, "Jul 10, 2025"}

Standard formats

Format:en:de:ja
:short"7/10/25""10.07.25""2025/07/10"
:medium"Jul 10, 2025""10.07.2025""2025/07/10"
:long"July 10, 2025""10. Juli 2025""2025年7月10日"
:full"Wednesday, July 10, 2025""Donnerstag, 10. Juli 2025""2025年7月10日木曜日"

Skeleton formats

Skeletons specify which fields to include; CLDR resolves the pattern for the locale.

Localize.Date.to_string(~D[2025-07-10], format: :yMMMEd, locale: :en)
#=> {:ok, "Thu, Jul 10, 2025"}

Localize.Date.to_string(~D[2025-07-10], format: :yMd, locale: :en)
#=> {:ok, "7/10/2025"}

Localize.Date.to_string(~D[2025-07-10], format: :yMMMd, locale: :en)
#=> {:ok, "Jul 10, 2025"}

Common date skeletons: :yMd, :yMMMd, :yMMMEd, :yMMM, :yMMMM, :MMMd, :MMMEd, :Md, :MEd.

Custom pattern strings

Localize.Date.to_string(~D[2025-07-10], format: "dd/MM/yyyy", locale: :en)
#=> {:ok, "10/07/2025"}

Localize.Date.to_string(~D[2025-07-10], format: "EEEE d MMMM y", locale: :en)
#=> {:ok, "Wednesday 10 July 2025"}

Partial dates (maps)

Localize.Date.to_string(%{year: 2025, month: 7}, format: :yMMM, locale: :en)
#=> {:ok, "Jul 2025"}

Time formatting

Localize.Time.to_string(~T[14:30:45], prefer: :ascii)
#=> {:ok, "2:30:45 PM"}

Standard formats

Format:en (:ascii):de:ja
:short"2:30 PM""14:30""14:30"
:medium"2:30:45 PM""14:30:45""14:30:45"

The :prefer option selects Unicode (default) or ASCII AM/PM markers:

Localize.Time.to_string(~T[14:30:00], locale: :en, prefer: :ascii)
#=> {:ok, "2:30:00 PM"}

Localize.Time.to_string(~T[14:30:00], locale: :en, prefer: :unicode)
#=> {:ok, "2:30:00\u202FPM"}  # narrow no-break space before PM

DateTime formatting

Localize.DateTime.to_string(~N[2025-07-10 14:30:45], prefer: :ascii)
#=> {:ok, "Jul 10, 2025, 2:30:45 PM"}

Standard formats

Format:en (:ascii):de (:ascii)
:short"7/10/25, 2:30 PM""10.07.25, 14:30"
:medium"Jul 10, 2025, 2:30:45 PM""10.07.2025, 14:30:45"
:long"July 10, 2025, 2:30:45 PM""10. Juli 2025, 14:30:45"

Separate date and time formats

Localize.DateTime.to_string(~N[2025-07-10 14:30:45],
  date_format: :full, time_format: :short, locale: :en, prefer: :ascii)
#=> {:ok, "Wednesday, July 10, 2025, 2:30 PM"}

Relative time formatting

Localize.DateTime.Relative.to_string(-60, locale: :en)
#=> {:ok, "1 minute ago"}

Localize.DateTime.Relative.to_string(3600, locale: :en)
#=> {:ok, "in 1 hour"}

Localize.DateTime.Relative.to_string(-86400, locale: :en)
#=> {:ok, "yesterday"}

Localize.DateTime.Relative.to_string(604800, locale: :en)
#=> {:ok, "next week"}

Interval formatting

Localize.Interval.to_string(~D[2025-04-22], ~D[2025-04-25], locale: :en)
#=> {:ok, "Apr 22 – 25, 2025"}

Localize.Interval.to_string(~D[2025-01-15], ~D[2025-03-20], locale: :en)
#=> {:ok, "Jan 15 – Mar 20, 2025"}

Date field symbols quick reference

SymbolMeaning1 char2 chars3 chars4 chars
yYear202525
MMonth707JulJuly
dDay of month101
EDay nameThuThuThuThursday
GEraADADADAnno Domini
QQuarter303Q33rd quarter

Time field symbols quick reference

SymbolMeaningExample
hHour 1–122 / 02
HHour 0–2314
mMinute5 / 05
sSecond9 / 09
aAM/PMPM

Common options

OptionValuesDefault
:localeatom, string, LanguageTagLocalize.get_locale()
:format:short, :medium, :long, :full, skeleton atom, or pattern string:medium
:prefer:unicode, :ascii:unicode