Home Date format string cheat sheet in Ruby?
Post
Cancel

Date format string cheat sheet in Ruby?

Date Formatting 
%YYear with century (e.g. 2022, 1982, etc)
%mMonth of the year, zero-padded (01..12)
%BThe full month name (e.g. January)
%bThe abbreviated month name (e.g. Jan)
%dDay of the month, zero-padded (01..31)
%jDay of the year (001..366)
%CYear / 100 (round down. e.g. 20 in 2013)
%yYear % 100 (00…99)
%_mMonth of the year, blank-padded (_1..12)
%-mMonth of the year, no-padding (1..12)
%^BThe full month name uppercased (e.g. JANUARY)
%^bThe abbreviated month name uppercased (e.g. JAN)
%hEquivalent to %b (abbreviated month name)
%-dDay of the month, no-padding (1..31)
%eDay of the month, blank-padded (_1..31)
Time Formatting 
%HHour of the day, 24-hour clock, zero-padded (00..23)
%kHour of the day, 24-hour clock, blank-padded (_0..23)
%IHour of the day, 12-hour clock, zero-padded (01..12)
%lHour of the day, 12-hour clock, blank-padded (_1..12)
%PMeridian indicator, lowercase (am or pm)
%pMeridian indicator, uppercase (AM or PM)
%MMinute of the hour (00..59)
%SSecond of the minute (00..59)
%LMillisecond of the second (000..999)
%NNanosecond (9 digits)
%3NMillisecond (3 digits)
%6NMicrosecond (6 digits)
%12NPicosecond (12 digits)
%15NFemtosecond (15 digits)
%18NAttosecond (18 digits)
%21NZeptosecond (21 digits)
%24NYoctosecond (24 digits)
Seconds Formatting 
%sNumber of seconds since 1970-01-01 00:00:00 UTC
%QNumber of milliseconds since 1970-01-01 00:00:00 UTC
Time Zone Formatting 
%zTime zone as hour and minute offset from UTC (e.g. +0900)
%:zTime zone hour and minute offset from UTC with a colon (e.g. +09:00)
%::zTime zone hour, minute and second offset from UTC (e.g. +09:00:00)
%:::zTime zone hour, minute and second offset from UTC (e.g. +09, +09:30, +09:30:30)
%ZTime zone abbreviation name or something similar information
Weekday Formatting 
%AThe full weekday name (e.g. Sunday)
%^AThe full weekday name uppercased (e.g. SUNDAY)
%aThe abbreviated weekday name (e.g. Sun)
%^aThe abbreviated weekday name uppercased (e.g. SUN)
%uDay of the week starting Monday (1..7)
%wDay of the week starting Sunday (0..6)
Week Formatting 
%GThe week-based year
%gThe last 2 digits of the week-based year (00..99)
%VWeek number of the week-based year (01..53)
%UWeek number of the year. Week starts with Sunday (00..53)
%WWeek number of the year. Week starts with Monday (00..53)
Flags 1 
-Don’t pad a numerical output
_Use spaced for padding
0Use zeros for padding
^Upcase the result string
#Change case
:Use colons for %z
Literal Strings 
%nNewline character (\n)
%tTab character (\t)
%%Literal ‘%’ character
Combination 
%cDate and time (%a %b %e %T %Y)
%DDate (%m/%d/%y)
%FThe ISO 8601 date format (%Y-%m-%d)
%vVMS date (%e-%b-%Y)
%xSame as %D
%XSame as %T
%r12-hour time (%I:%M:%S %p)
%R24-hour time (%H:%M)
%T24-hour time (%H:%M:%S)
%+date(1) (%a %b %e %H:%M:%S %Z %Y)
This post is licensed under CC BY 4.0 by the author.