Forum: Ruby on Rails Getting number of days in a month

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Dylan M. (Guest)
on 2006-03-01 01:44
(Received via mailing list)
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).
Eric G. (Guest)
on 2006-03-01 01:55
(Received via mailing list)
Hey,
This should work.

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

Cheers,
Eric G.


Dylan M. wrote:
>
> 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
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails


--
Eric G.
http://www.ericgoodwin.com
Kent S. (Guest)
on 2006-03-01 01:57
(Received via mailing list)
$ ./script/console
Loading development environment.
>> Time.days_in_month 2
=> 28
>> Time.days_in_month 2, 2008
=> 29
>>

Kent
Eric G. (Guest)
on 2006-03-01 01:59
(Received via mailing list)
oops ...

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

Eric G. wrote:
>> In PHP, there was an argument you could pass to the Date function to
>> 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
>> removed_email_address@domain.invalid
>> http://lists.rubyonrails.org/mailman/listinfo/rails
>
>


--
Eric G.
http://www.ericgoodwin.com
Bob S. (Guest)
on 2006-03-01 02:21
(Received via mailing list)
How about using:

Time.days_in_month(month, year=nil)

Bob S.
http://www.railtie.net/
Dylan M. (Guest)
on 2006-03-01 05:26
(Received via mailing list)
Beautiful! Thank you everyone!
This topic is locked and can not be replied to.