Current location: Hot Scripts Forums » Programming Languages » Perl » Date string check


Date string check

Reply
  #1 (permalink)  
Old 04-26-05, 10:32 PM
roby1234 roby1234 is offline
New Member
 
Join Date: Apr 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Date string check

Hi - I'm new to perl and I am trying to do a date string check. What I want is my script to check for the following - 4/12/2005 or 04/12/2005 or 4/12/05 or 04/12/05. Is this correct:

($date =~ m/[\2d]+\/[\2d]+\/[\4d]+/) or

($date =~ m/[\02d]+\/[\02d]+\/[\4d]+/)or

($date =~ m/[\2d]+\/[\2d]+\/[\2d]+/)or

($date =~ m/[\02d]+\/[\02d]+\/[\4d]+/)

Then if it is correct I want it to convert the date to: April 12, 2005, which I haven't figured out yet. Any suggestions would be greatly appreciated.

Thank you in advance.
Reply With Quote
  #2 (permalink)  
Old 04-29-05, 03:55 PM
Chas Chas is offline
Coding Addict
 
Join Date: Oct 2003
Location: California
Posts: 359
Thanks: 0
Thanked 0 Times in 0 Posts
This is kind of a bit overkill but I'm lazy when it comes to dates and times:

Code:
#!/usr/bin/perl
use strict;
use warnings;
use Time::Piece;

my @dates = qw(4/12/2005 04/12/2005 4/12/05 04/12/05);

print "Content-type: text/html\n\n";
print "<pre>\n";

foreach my $date (@dates) {
  my ($m, $d, $y) = split m!/!, $date;
  my $fmt = $y > 100 ? "%m/%d/%Y" : "%m/%d/%y";
  print "Date: $date => ";
  print Time::Piece->strptime($date, $fmt)->strftime("%b %d, %Y") . "\n";
}

print "</pre>\n";
You have the year, month and day in the first line and you can do some simple operations on it then use a hash x-ref for month number to month name to format it correctly. I just prefer to abstract all that using something like Time:iece or Class:ate.

~Charlie
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP syntax error stuckonaproject ASP 8 12-14-04 10:30 AM
js date franches JavaScript 1 11-25-04 08:42 AM
Urgent converting String to Date yymae Everything Java 1 01-16-04 11:22 AM
Declared Functions skipper23 PHP 4 12-17-03 10:06 AM
index page not showing up skipper23 PHP 3 12-15-03 01:10 PM


All times are GMT -5. The time now is 04:03 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.