# date calculation in Perl

I have to compare two input dates format MM/DD/YYYY and calculate how many days are between both dates
For example
D1.  12/05/2007
D2.  12/04/2006

Difference is 366 days.
Is there any subroutine that I could use ?
I tried to split the dates and do the calculations myself, but I doesn't work all the time.

Asked:
###### Who is Participating?

Commented:
use Time::Local;
my (\$m1,\$d1,\$y1)=split/\//,"12/05/2007";
my(\$m2,\$d2,\$y2)=split/\//,"12/04/2006";
print ((timegm(0,0,12,\$d1,\$m1-1,\$y1-1900)-timegm(0,0,12,\$d2,\$m2-1,\$y2-1900))/(24*60*60),"\n");

0

QA ManagerCommented:
use Date::Calc;

\$Dd = Delta_Days(\$year1,\$month1,\$day1, \$year2,\$month2,\$day2);

This function returns the difference in days between the two given dates.

The result is positive if the two dates are in chronological order, i.e., if date #1 comes chronologically BEFORE date #2, and negative if the order of the two dates is reversed.

The result is zero if the two dates are identical.
0

Author Commented:
So I'm still going to have to split the date on "/" and then put them into a Month, Day, Year variable and put them in the date_delta formula. Is there no way to read in the date "9/6/1997" and have the variables automatically assigned to Month, Day, Year?
0

Commented:
(\$month,\$day,\$year)= split/\//,"9/6/1997"
0

Commented:
You could also use the Date::Manip module.  This would likely be slower though.
\$Delta = DateCalc(\$date1, \$date2);
See here for more details: http://search.cpan.org/~sbeck/Date-Manip-5.48/Manip.pod

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

## Already a member? Login.

All Courses

From novice to tech pro — start learning today.