The blog has moved to http://jessehouse.com/ ... Many google searches point here so I am leaving it operational, but there will be no new posts.

Friday, November 5, 2010

Get the date of the beginning and end of a month using Ruby

Sometimes Ruby blows my mind, I needed to get the start date and end date of the previous month. Guess what Ruby just happens to have methods to handle this - beginning_of_month, end_of_month, there is even beginning_of_quarter and end_of_quarter if you need them!


last_month = Date.today.last_month # => Tue, 05 Oct 2010
last_month.beginning_of_month # => Fri, 01 Oct 2010
last_month.end_of_month # => Sun, 31 Oct 2010
last_month.beginning_of_quarter # => Fri, 01 Oct 2010
last_month.end_of_quarter # => Fri, 31 Dec 2010


No comments: