Ruby on Rails


Calculate the Last Day of Month with Ruby

Posted by Jamal Nasir on February 7, 2012 at 3:45 AM

Date.new(Time.now.year, 12, 31)

The above line will create a date with the current year, 12 months and 31 days.


This will subtract the current month number of month from 12. For example;

If it is Febuary (2), the above line of code will give you 10.


last_day_of_month= (Date.new(Time.now.year, 12, 31) << (12-Time.now.month)).day

So, the above line will give you the last day of month.

Categories: None

Post a Comment


Oops, you forgot something.


The words you entered did not match the given text. Please try again.

Already a member? Sign In

1 Comment

Reply Rq Bukhari
4:00 AM on February 27, 2012 
Thank you so much for sharing this useful post. Keep sharing your knowledge with us.