Days in a year

How do I get number of days in a year?

Pål Bergström wrote:

How do I get number of days in a year?

Enter “number of days in a year” into Google and you’ll get some useful
results.

The very first hit gives you

Google is your friend.

Just check if the year is leap. In google u’ll find something like:

int __isleap <javascript:searchRef(’__isleap’)>(int
year<javascript:searchRef(‘year’)>)
{
/* every fourth year is a leap year except for century years that are

  • not divisible by 400. /
    /
    return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); */
    return (!(year <javascript:searchRef(‘year’)>%4) &&
    ((year<javascript:searchRef(‘year’)>%100)
    || !(year <javascript:searchRef(‘year’)>%400)));
    }

On Mon, Oct 27, 2008 at 9:12 AM, Pål Bergström [email protected] wrote:

How do I get number of days in a year?

You can use the date library…

require ‘date’; puts Date.new(2005) - Date.new(2004)
=> 366

Todd

Todd B. wrote:

On Mon, Oct 27, 2008 at 9:12 AM, P�l Bergstr�m [email protected] wrote:

How do I get number of days in a year?

You can use the date library…

require ‘date’; puts Date.new(2005) - Date.new(2004)
=> 366

Todd

So there’s nothing like this (Rails)

Time.days_in_month()

A stupid question in the first place. I’ve done like this; I check if
Feb in a particular year has 29 days or not, so it gives 365 or 366
days. Simple. :slight_smile:

On Mon, Oct 27, 2008 at 1:20 PM, PÃ¥l Bergström [email protected]
wrote:

So there’s nothing like this (Rails)

Time.days_in_month()

You could just “require ‘activesupport’”

or look at the code in activesupport…

condensed for your immediate use!

class Time
COMMON_YEAR_DAYS_IN_MONTH = [nil, 31, 28, 31, 30, 31, 30, 31, 31,
30, 31, 30, 31]
class << self
# Return the number of days in the given month.
# If no year is specified, it will use the current year.
def days_in_month(month, year = now.year)
return 29 if month == 2 && ::Date.gregorian_leap?(year)
COMMON_YEAR_DAYS_IN_MONTH[month]
end
end
end

Pål Bergström wrote:

A stupid question in the first place. I’ve done like this; I check if
Feb in a particular year has 29 days or not, so it gives 365 or 366
days. Simple. :slight_smile:

And notice how days_in_month is implemented in activesupport:

      def days_in_month(month, year = now.year)
        return 29 if month == 2 && ::Date.gregorian_leap?(year)
        COMMON_YEAR_DAYS_IN_MONTH[month]
      end

So all you need is:

irb(main):001:0> require ‘date’
=> true
irb(main):002:0> Date.gregorian_leap?(2008) ? 366 : 365
=> 366
irb(main):003:0> Date.gregorian_leap?(2009) ? 366 : 365
=> 365

Brian C. [email protected] wrote:

So all you need is:

irb(main):001:0> require ‘date’
=> true
irb(main):002:0> Date.gregorian_leap?(2008) ? 366 : 365 => 366
irb(main):003:0> Date.gregorian_leap?(2009) ? 366 : 365 => 365

Or ask the day of year of December 31st:

require ‘date’

DateTime.new(2008, 12, 31).yday # => 366

DateTime.new(2009, 12, 31).yday # => 365

Best regards,
Jan F.

Jan F. wrote:

Brian C. [email protected] wrote:

Or ask the day of year of December 31st:

require ‘date’

DateTime.new(2008, 12, 31).yday # => 366

DateTime.new(2009, 12, 31).yday # => 365

Best regards,
Jan F.

That was smart. Thanks!

Brian C. [email protected] wrote:

So all you need is:

irb(main):001:0> require ‘date’
=> true
irb(main):002:0> Date.gregorian_leap?(2008) ? 366 : 365 => 366
irb(main):003:0> Date.gregorian_leap?(2009) ? 366 : 365 => 365

Or ask the day of year of December 31st:

require ‘date’
DateTime.new(2008, 12, 31).yday # => 366
DateTime.new(2009, 12, 31).yday # => 366

Best regards,
Jan F.