Days in a year


#1

How do I get number of days in a year?


#2

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
http://en.wikipedia.org/wiki/Leap_year#Algorithm

Google is your friend.


#3

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)));
    }

#4

On Mon, Oct 27, 2008 at 9:12 AM, Pål Bergström removed_email_address@domain.invalid 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


#5

Todd B. wrote:

On Mon, Oct 27, 2008 at 9:12 AM, P�l Bergstr�m removed_email_address@domain.invalid 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()


#6

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:


#7

On Mon, Oct 27, 2008 at 1:20 PM, Pål Bergström removed_email_address@domain.invalid
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


#8

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


#9

Brian C. removed_email_address@domain.invalid 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.


#10

Jan F. wrote:

Brian C. removed_email_address@domain.invalid 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!


#11

Brian C. removed_email_address@domain.invalid 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.