[Nitro] [NP.ORG] Nitro/Og Fora daily digest

Bill Kelly billk at cts.com
Mon May 21 20:28:09 EDT 2007


From: "Reid Thompson" <reid.thompson at ateb.com>
> george.moschovitis at gmail.com wrote:
>>
>> is there an elegant way to find out if two Time objects represent
>> the same day, ie ignore the hh:mm:ss part of the Time object in
>> this comparisson?
>>   
> is
> 
>    t1 = Time.now
>    t2 = Time.now
> 
>    if (t1.day == t2.day)
>    then
>      puts "the days are the same"
>    else
>        puts "the days are different"
>    end
> 
>    t2 = t2 + 100323
> 
>    if (t1.day == t2.day)
>    then
>      puts "the days are the same"
>    else
>        puts "the days are different"
>    end
> 
> elegant????

Note that Time#day refers to the day of the month, such that:
(assuming a 31 day month)

irb(main):050:0> Time.now.day
=> 21
irb(main):051:0> (Time.now + (24 * 60 * 60 * 31)).day
=> 21

If that's not acceptable, and you only want the times to
be equal on the exact same day, you could do:

  if (t1.year == t2.year  &&  t1.yday == t2.yday)
   . . . 


Regards,

Bill





More information about the Nitro-general mailing list