Solved

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

Posted on 2004-09-26
8
221 Views
Last Modified: 2010-04-20
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
0
Comment
Question by:lynn402
  • 3
  • 3
  • 2
8 Comments
 
LVL 40

Expert Comment

by:jlevie
ID: 12156476
Try "if($date =~ /\d\d-\d\d-\d\d\d\d/ || $date =~ /\d\d\/\d\d\/\d\d\d\d/)"
0
 
LVL 4

Expert Comment

by:EinarTh
ID: 12159288
Correct expression is:

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

cheers.
0
 
LVL 4

Expert Comment

by:EinarTh
ID: 12159297
or even:

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

cheers
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

Author Comment

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

Author Comment

by:lynn402
ID: 12175743
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.....
}
0
 
LVL 40

Expert Comment

by:jlevie
ID: 12175813
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
0
 
LVL 40

Expert Comment

by:jlevie
ID: 12175826
> 9%2F18%2F2004
> #How can I  change the hex info back to the / so it will get recognized

$date =~ s/%2F/\//g;
0
 
LVL 4

Accepted Solution

by:
EinarTh earned 50 total points
ID: 12181223
Sorry, didn't notice you url-encoded the dates.

There is a cgi function to url-decode it, but I dont have perl in front of me right now.

In any case you can:

$date =~ s/%2F/\//gi;

and then use the expression in the previous comment.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

829 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