Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Days in a year

Reply
Thread Tools

Days in a year

 
 
Pål Bergström
Guest
Posts: n/a
 
      10-27-2008
How do I get number of days in a year?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Brian Candler
Guest
Posts: n/a
 
      10-27-2008
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.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Hlio Rocha
Guest
Posts: n/a
 
      10-27-2008
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 =3D=3D 0 && (year % 100 !=3D 0 || year % 400 =3D=3D 0)=
); */
return (!(year <javascript:searchRef('year')>%4) &&
((year<javascript:searchRef('year')>%100)
|| !(year <javascript:searchRef('year')>%400)));
}

On Mon, Oct 27, 2008 at 2:12 PM, P=E5l Bergstr=F6m <(E-Mail Removed)> w=
rote:

> How do I get number of days in a year?
> --
> Posted via http://www.ruby-forum.com/.
>
>


 
Reply With Quote
 
Todd Benson
Guest
Posts: n/a
 
      10-27-2008
On Mon, Oct 27, 2008 at 9:12 AM, P=E5l Bergstr=F6m <(E-Mail Removed)> w=
rote:
> 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)
=3D> 366

Todd

 
Reply With Quote
 
Pål Bergström
Guest
Posts: n/a
 
      10-27-2008
Todd Benson wrote:
> On Mon, Oct 27, 2008 at 9:12 AM, P�l Bergstr�m <(E-Mail Removed)> 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()
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Pål Bergström
Guest
Posts: n/a
 
      10-28-2008
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.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      10-28-2008
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.


And notice how days_in_month is implemented in activesupport:

def days_in_month(month, year = now.year)
return 29 if month == 2 && :ate.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?(200 ? 366 : 365
=> 366
irb(main):003:0> Date.gregorian_leap?(2009) ? 366 : 365
=> 365
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Jan Friedrich
Guest
Posts: n/a
 
      10-28-2008
Brian Candler <(E-Mail Removed)> wrote:
> So all you need is:
>
> irb(main):001:0> require 'date'
> => true
> irb(main):002:0> Date.gregorian_leap?(200 ? 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 Friedrich
 
Reply With Quote
 
Jan Friedrich
Guest
Posts: n/a
 
      10-28-2008
Brian Candler <(E-Mail Removed)> wrote:
> So all you need is:
>
> irb(main):001:0> require 'date'
> => true
> irb(main):002:0> Date.gregorian_leap?(200 ? 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 Friedrich
 
Reply With Quote
 
Pål Bergström
Guest
Posts: n/a
 
      10-28-2008
Jan Friedrich wrote:
> Brian Candler <(E-Mail Removed)> 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 Friedrich


That was smart. Thanks!
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
2 year AAS degree (w/certs) vs. 4 year CIS degree smolderingwick Microsoft Certification 0 02-06-2011 04:44 PM
IE 'unsafe' for 284 days last year Au79 Computer Support 2 01-06-2007 01:12 PM
Week of year to full Year Otuatail HTML 4 12-08-2003 07:50 PM
getYear() returns correct year local, but wrong year online Veerle Javascript 2 08-20-2003 06:37 PM



Advertisments