Solved

# How to comput the date

Posted on 2001-08-01
159 Views
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
Question by:kplonk
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 8

Accepted Solution

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

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

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

ID: 6346603
Just th job
0

## Featured Post

Question has a verified solution.

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

### Suggested Solutions

Perl script to process a .csv file 18 84
Adding a countdown to HTA 12 113
Need help Creating PowerShell Script 4 66
Merging text files strings with filename 18 55
This article will show, step by step, how to integrate R code into a R Sweave document
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will learn how to count occurrences of each item in an array.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
###### Suggested Courses
Course of the Month7 days, 5 hours left to enroll