Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > finding differences between dates

Reply
Thread Tools

finding differences between dates

 
 
Joseph Paish
Guest
Posts: n/a
 
      06-24-2005
i have 2 dates in mm/dd/yyyy format and i need to find the number of days in
between them. i have googled both the web and the ruby newsgroup and haven't
found anything that addresses this.

for what it's worth, this is what the perl code looks like that i am trying to
convert to ruby :


#!/usr/bin/perl
use Time::Local ; # for days_between subroutine

################################################## #
# return the absolute value of the difference between two dates

sub days_between {

my $first_date = $_[0] ;
my $second_date = $_[1] ;

my ($month1, $day1, $year1) = (split /\//, $first_date) ;
my ($month2, $day2, $year2) = (split /\//, $second_date) ;

my $date1 = timelocal 0, 0, 0, $day1, $month1 - 1, $year1 - 1900 ;
my $date2 = timelocal 0, 0, 0, $day2, $month2 - 1, $year2 - 1900 ;
return abs(($date2 - $date1) / 86400) ; # the number of seconds in a day
}
################################################## #


thanks

joe



 
Reply With Quote
 
 
 
 
Paul Brannan
Guest
Posts: n/a
 
      06-24-2005
Use the Date class:

irb(main):008:0> require 'date'
=> true
irb(main):009:0> Date.parse('11/05/1994') - Date.parse('3/7/1988')
=> Rational(2434, 1)



 
Reply With Quote
 
 
 
 
Joseph Paish
Guest
Posts: n/a
 
      06-24-2005
On June 24, 2005 10:33, Paul Brannan wrote:
> Use the Date class:
>
> irb(main):008:0> require 'date'
> => true
> irb(main):009:0> Date.parse('11/05/1994') - Date.parse('3/7/1988')
> => Rational(2434, 1)


interesting.

my primary source for ruby information is the first edition of the Pickaxe
book. didn't find anything about "parse" under Date. now i know.

thank you

joe






 
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
Dates dates dates dates... SQL and ASP.NET David Lozzi ASP .Net 1 09-30-2005 02:18 PM
Dates! Dates! Dates! PW ASP General 4 08-09-2004 04:42 PM
Re: App for finding differences between lots of images? East75th Digital Photography 0 07-04-2003 06:24 AM
Re: App for finding differences between lots of images? GordonG Digital Photography 0 07-04-2003 06:04 AM
Re: App for finding differences between lots of images? Warren Sarle Digital Photography 0 07-04-2003 06:00 AM



Advertisments