Solved

date calculation in Perl

Posted on 2007-12-05
5
2,734 Views
Last Modified: 2008-02-01
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.

0
Comment
Question by:oaihuynh
5 Comments
 
LVL 9

Assisted Solution

by:Suhas .
Suhas . earned 62 total points
Comment Utility
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 Comment

by:oaihuynh
Comment Utility
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
 
LVL 84

Expert Comment

by:ozo
Comment Utility
($month,$day,$year)= split/\//,"9/6/1997"
0
 
LVL 39

Expert Comment

by:Adam314
Comment Utility
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
 
LVL 84

Accepted Solution

by:
ozo earned 63 total points
Comment Utility
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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now