Posts Tagged ‘ruby’

Date formatting in Ruby [TECH]

Thursday, February 21st, 2008

I found something…frustrating(?) in Ruby today. Date formatting was not as intuitive as I expected, but this is probably due to my php experience. So, very intuitive, simple Date.today does exactly what you expect (an object of today’s date). Date.today.month gives you the month…number. So how do I get the natural language month? Well there is this array in the Date class that keeps the months in it. Date.today.month gives you the number, so you just get the name of it based on that array. Solution:

Date::MONTHNAMES[Date.today.month]

By all means if this is not the easiest way to do this, let me know.