Link to home
Start Free TrialLog in
Avatar of lynn402
lynn402

asked on

Program Won't Record Dates Such as 9/23/2004

I Have this typed in my perl code:
use CGI qq(:standard escapeHTML);

if ($date=~ /\d\d?[-/]/d/d?[-/]\d\d\d\d/){
do this......
this......
and this.....
}

It works when I have 9-23-2004 but not when I have 9/23/2004

I have a subroutine that changes the date to a format mysql understands. 2004-09-22

I have tried escaping / with \/ but it still doesn''work
Avatar of jlevie
jlevie

Try "if($date =~ /\d\d-\d\d-\d\d\d\d/ || $date =~ /\d\d\/\d\d\/\d\d\d\d/)"
Correct expression is:

/\d\d?[-\/]\d\d?[-\/]\d\d\d\d/

cheers.
or even:

/(\d{1,2}[-\/]){2}\d{4}/

cheers
Avatar of lynn402

ASKER

It still doesn't work, EinarTh, I've tried \d\d?[-\/] and it never worked. jlieve, your suggestion didn't work either.
Avatar of lynn402

ASKER

I'm using Perl's CGI.pm

use CGI; qq(:starndard );
#the date is located inside  query_string
$date=query_string;


print $date
#before the if statement executed I printed $date and got when the date was 9/28/2004
9%2F18%2F2004
#How can I  change the hex info back to the / so it will get recognized

if ($date=~ /\d\d?[-/]/d/d?[-/]\d\d\d\d/){
do this......
this......
and this.....
}
Oops, I just realized that my comment wasn't exactly right for the data you show in the question. It should have been:

if($date =~ /\d{1,2}-\d{1,2}-\d{2,4}/ || $date =~ /\d{1,2}\/\d{1,2}\/\d{2,4}/)

I was looking at the SQL date representation which uses 2 digit month & day and 4 digit years. The if statement above is correct in the general case as shown by:

#!/usr/bin/perl
$date[0] = "9-27-2004";
$date[1] = "09-27-04";
$date[2] = "9/28/2004";
$date[3] = "09/1/04";

for($i=0; $i<=3; $i++)
{
  $date = $date[$i];
  if($date =~ /\d{1,2}-\d{1,2}-\d{2,4}/ || $date =~ /\d{1,2}\/\d{1,2}\/\d{2,4}/)
  {
    print "Date ($date) valid\n";
  }
  else
  {
    print "Date ($date) invalid\n";
  }
}

chaos> perl dates-check
Date (9-27-2004) valid
Date (09-27-04) valid
Date (9/28/2004) valid
Date (09/1/04) valid
> 9%2F18%2F2004
> #How can I  change the hex info back to the / so it will get recognized

$date =~ s/%2F/\//g;
ASKER CERTIFIED SOLUTION
Avatar of EinarTh
EinarTh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial