[Rails] Getting number of days in a month

Eric Goodwin ruby at ericgoodwin.com
Wed Mar 1 00:00:18 GMT 2006


Hey,
This should work.

@number_of_days = Date.civil(year, month, -1).day

Cheers,
Eric Goodwin


Dylan Markow wrote:
> In PHP, there was an argument you could pass to the Date function to 
> get the number of days in the current month:
>
> echo date("t"); // Outputs "28" for February
>
> I don't see anything like this in Ruby/Rails. Right now, I'm using a 
> very ugly line to pull the last day of the month:
>
> @number_of_days = (Date.strptime(Date.today.strftime("%Y-%m-01")) >> 
> 1) - 1
>
> Basically, it takes the first day of the current month, adds one 
> month, and then subtracts one day. Am I completely overlooking some 
> obvious and easy way to do this? (I'm assuming so).
>
>
> _______________________________________________
> Rails mailing list
> Rails at lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails


-- 
Eric Goodwin
http://www.ericgoodwin.com



More information about the Rails mailing list