finding differences between dates

Discussion in 'Ruby' started by Joseph Paish, Jun 24, 2005.

  1. Joseph Paish

    Joseph Paish Guest

    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
    Joseph Paish, Jun 24, 2005
    #1
    1. Advertising

  2. Joseph Paish

    Paul Brannan Guest

    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)
    Paul Brannan, Jun 24, 2005
    #2
    1. Advertising

  3. Joseph Paish

    Joseph Paish Guest

    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
    Joseph Paish, Jun 24, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Lozzi

    Dates dates dates dates... SQL and ASP.NET

    David Lozzi, Sep 29, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    840
    Rob Schieber
    Sep 30, 2005
  2. Home_Job_opportunity
    Replies:
    0
    Views:
    482
    Home_Job_opportunity
    Jan 8, 2009
  3. Home_Job_opportunity
    Replies:
    0
    Views:
    568
    Home_Job_opportunity
    Jan 14, 2009
  4. PW

    Dates! Dates! Dates!

    PW, Aug 7, 2004, in forum: ASP General
    Replies:
    4
    Views:
    175
    Mark Schupp
    Aug 9, 2004
  5. Replies:
    1
    Views:
    194
    Jano Svitok
    Jul 17, 2007
Loading...

Share This Page