[Rails] Getting number of days in a month

Dylan Markow dylan at dylanmarkow.com
Wed Mar 1 03:26:17 GMT 2006


Beautiful! Thank you everyone!

On Feb 28, 2006, at 4:23 PM, Bob Silva wrote:

> How about using:
>
> Time.days_in_month(month, year=nil)
>
> Bob Silva
> http://www.railtie.net/
>
>
> -----Original Message-----
> From: rails-bounces at lists.rubyonrails.org
> [mailto:rails-bounces at lists.rubyonrails.org] On Behalf Of Eric Goodwin
> Sent: Tuesday, February 28, 2006 4:03 PM
> To: rails at lists.rubyonrails.org
> Subject: Re: [Rails] Getting number of days in a month
>
> oops ...
>
> today = Date.today
> @number_of_days = Date.civil(today.year, today.month, -1).day
>
> Eric Goodwin wrote:
>> 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
>
> _______________________________________________
> Rails mailing list
> Rails at lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
> _______________________________________________
> Rails mailing list
> Rails at lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>



More information about the Rails mailing list