pencil writing a date
software development ruby engineering
December 9th, 2021 - Erhan Berber

Formatting Dates in Ruby

When converting Date and DateTime objects to strings for display using a function like strftime, here's a handy reference sheet for the options you can use to format output.

Here's an example implementation to display a date in the mm/dd/yyyy format.

> the_date.strftime('%m/%d/%Y')
=> 12/10/2021

Ruby Date and Time Format Options

Format Description Example
%b Shortened month name "Jan"
%B Full month name "January"
%m Numbered month of the year (01..12) "01"
%d Day of the month (01..31) "15"
%j Day of the year (001..366) "123"
%a Shortened weekday name "Mon"
%A Full weekday name "Monday"
%w The numeric day of the week (0..6) "1"
%U Numeric week number in the year (0..53) [starting with the first Sunday] "22"
%W Numeric week number in the year (0..53) [starting with the first Monday] "22"
%y Two-digit year (00..99) "99"
%Y Four-digit year "2022"
%H Hour of the day (military time) (00..23) "18"
%I Hour of the day (12-hour clock) (01..12) "08"
%M Minute of the hour (0..59) "15"
%S Second of the minute (0..59) "30"
%p AM or PM "PM"
%Z Time zone name "Central Time (US & Canada)"


Contact Us

Have questions? Reach out. We're happy to chat about who we are, what we do, and how we can partner with your organization to better utilize custom software in achieving your business goals. Drop us a line and let's chat.