Get last day of the month in Ruby Get last day of the month in Ruby ruby ruby

Get last day of the month in Ruby


use Date.civil

With Date.civil(y, m, d) or its alias .new(y, m, d), you can create a new Date object. The values for day (d) and month (m) can be negative in which case they count backwards from the end of the year and the end of the month respectively.

=> Date.civil(2010, 02, -1)=> Sun, 28 Feb 2010>> Date.civil(2010, -1, -5)=> Mon, 27 Dec 2010


To get the end of the month you can also use ActiveSupport's helper end_of_month.

# Require extensions explicitly if you are not in a Rails environmentrequire 'active_support/core_ext' p Time.now.utc.end_of_month # => 2013-01-31 23:59:59 UTCp Date.today.end_of_month   # => Thu, 31 Jan 2013

You can find out more on end_of_month in the Rails API Docs.


So I was searching in Google for the same thing here...

I wasn't happy with above so my solution after reading documentation in RUBY-DOC was:

Example to get 10/31/2014

Date.new(2014,10,1).next_month.prev_day