Meanwhile, back at the farm

The personal blog of Michael Farmer

Comprehensive Ruby Date/Datetime Formatting Strings

Comments

I’ve searched high and low in the ruby documentation for a comprehensive list of the Date and Datetime formatting strings and haven’t been able to find it.  Because I use this so often, I decided to post it on my blog for my own reference.  I hope any other readers will find it helpful as well.

%a – The abbreviated weekday name (`Sun’)
%A – The  full  weekday  name (`Sunday’)
%b – The abbreviated month name (`Jan’)
%B – The  full  month  name (`January’)
%c – The preferred local date and time representation
%C – century number (the year divided by 100 and truncated to an integer, range 00 to 99)
%d – Day of the month (01..31)
%D – same as %m/%d/%y
%e – day of the month as a decimal number, a single digit is preceded by a space (range ‘1′ to ‘31′)
%g – like %G, but without the century.
%G – The 4-digit year corresponding to the ISO week number (see %V). This has the same format and value as %Y, except that if the ISO week number to the previous or next year, that year is used instead.
%h – same as %b
%H – Hour of the day, 24-hour clock (00..23)
%I – Hour of the day, 12-hour clock (01..12)
%j – Day of the year (001..366)
%m – Month of the year (01..12)
%M – Minute of the hour (00..59)
%n – newline character
%p – Meridian indicator (“AM”  or  “PM”)
%r – time in a.m. and p.m. notation
%R – time in 24 hour notation
%S – Second of the minute (00..60)
%t – tab character
%T – current time, equal to %H:%M:%S
%u – weekday as a decimal number [1,7], with 1 representing Monday
%U – Week  number  of the current year, starting with the first Sunday as the first day of the first week (00..53)
%V – The ISO 8601:1988 week number of the current year as a decimal number, range 01 to 53, where week 1 is the first week that has at least 4 days in the current year, and with Monday as the first day of the week. (Use %G or %g for the year component that corresponds to the week number for the specified timestamp.)
%W – Week  number  of the current year, starting with the first Monday as the first day of the first week (00..53)
%w – Day of the week (Sunday is 0, 0..6)
%x – Preferred representation for the date alone, no time
%X – Preferred representation for the time alone, no date
%y – Year without a century (00..99)
%Y – Year with century
%Z or %z – Time zone name
%% – Literal character %

To use these formatting strings use the strftime and strptime methods of the Date and Datetime classes.

Reblog this post [with Zemanta]

Written by mikefarmer

September 18th, 2009 at 11:37 am

Posted in Development

Tagged with

blog comments powered by Disqus