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).
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@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-- Eric Goodwin http://www.ericgoodwin.com
$ ./script/console Loading development environment.>> Time.days_in_month 2=> 28>> Time.days_in_month 2, 2008=> 29>>Kent On 2/28/06, Dylan Markow <dylan@dylanmarkow.com> 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@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
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@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails > >-- Eric Goodwin http://www.ericgoodwin.com
How about using: Time.days_in_month(month, year=nil) Bob Silva http://www.railtie.net/ -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Eric Goodwin Sent: Tuesday, February 28, 2006 4:03 PM To: rails@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@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails > >-- Eric Goodwin http://www.ericgoodwin.com _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails
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@lists.rubyonrails.org > [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Eric Goodwin > Sent: Tuesday, February 28, 2006 4:03 PM > To: rails@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@lists.rubyonrails.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> > > > -- > Eric Goodwin > http://www.ericgoodwin.com > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >