Solved

How to comput the date

Posted on 2001-08-01
4
155 Views
Last Modified: 2013-12-25
I have a date string like this

dd/mm/yyyy

and i need to be able to do computations on it, such as is this date newer than this one

both dates are strings in the above format

how do i do this in perl
0
Comment
Question by:kplonk
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
bebonham earned 50 total points
ID: 6342987
#a non module idea for such specific strings
#note pretty, but pretty fast

###just to show it works
chomp($date=<STDIN>);  #for dates greater than 1999
chomp($futureDate=<STDIN>);
chomp($pastDate=<STDIN>);
print cmpDate($date,$futureDate);
print "\n" . cmpDate($date, $pastDate);
print "\n" . cmpDate($futureDate, $date);
####


###this is the real function

sub cmpDate  #returns 1 for 2nd arg is greater, == 0, < -1
{
my($a,$b)=@_;
my($daya,$montha,$yeara)=split('/', $a);
my($dayb,$monthb,$yearb)=split('/', $b);
if($yeara>$yearb){ return -1};
if($yeara<$yearb){ return 1};
if($montha>$monthb){ return -1};
if($montha<$monthb){ return 1};
if($daya>$dayb){ return -1};
if($daya<$dayb){ return 1};
return 0;  #careful, returns 0 on error as well.
}
######
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6342994
actually, no, since perl is so flexable, it will return the greatest

1/2/3

3 ranked highest
then 1
then 2

0
 
LVL 84

Expert Comment

by:ozo
ID: 6343321
return $yeara <=> $yearb or $montha <=> $monthb or $daya <=> $dayb;

#or

return (join'',reverse split'/',$a) cmp (join'',reverse split'/',$b);
0
 

Author Comment

by:kplonk
ID: 6346603
Just th job
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Ever wondered how to display how many visitors you have online. In this tutorial I will show you an easy but effective way to display the number of online visitors in WhizBase. In this article I assume you have read my previous articles and know …
If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

911 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

22 Experts available now in Live!

Get 1:1 Help Now