Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi.

I've got two variables with different dates (in the format of mm/dd/yyyy).

Does anyone know how I can calculate the difference (in days) between the first and second date?

Thanks,
Ralph.

Replies are listed 'Best First'.
Re: Calculate Time Range
by larsen (Parson) on Jun 16, 2002 at 15:27 UTC
Re: Calculate Time Range
by greenFox (Vicar) on Jun 17, 2002 at 10:31 UTC
    Another way is to use Time::Local, it has the advantage of being a part of core perl and works well when the incoming data has a known date format. A simple example-

    use Time::Local; my ($month, $mday, $year) = split "/", "06/08/2002"; my ($month2, $mday2,$year2) = split "/", "06/18/2002"; my $time = timelocal(0,0,0,$mday,$month-1,$year); my $time2 = timelocal(0,0,0,$mday2,$month2-1,$year2); my $diff = abs(($time - $time2)/(60*60*24)); print "diff = $diff\n";

    You need to remember that the months are base 0. I grabbed the abs() difference but maybe you want to know if the difference is negative...

    --
    my $chainsaw = 'Perl';

Re: Calculate Time Range
by Massyn (Hermit) on Jun 17, 2002 at 01:46 UTC
    I've written a calc_time procedure (available from http://phillipmassyn.tripod.com) that will convert the date to it's numeric form. Once you've done this on both dates, you can just subtract the two values from each other. What you'll be left with, is the number of seconds from day 1 to day 2, and then just divide by 86400.