• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 496
  • Last Modified:

How to check if date is valid in perl?

My date format is dd/mm/yyyy

This date should return an error:  30/31/2009

How can I check this date and return that it does not exist.
0
dmalovich
Asked:
dmalovich
  • 2
  • 2
1 Solution
 
farzanjCommented:
Try
use DateTime::Format::ISO8601;
eval {
    $date = DateTime::Format::ISO8601->parse_datetime("20061730");
};
if ( $@ ) { print "ERROR: date not valid" };

Open in new window

0
 
dmalovichAuthor Commented:
What does "20061730" represent?
0
 
farzanjCommented:
That is a date in YYYYMMDD format.  
You can get it like

my $date = '30/31/2009';
my ($dd, $mm, $yyyy) = $date =~ m{(\d\d)/(\d\d)/(\d\d\d\d)};

my $yyyymmdd = "$yyyy$mm$dd";

You can also write test conditions yourself as well
0
 
dmalovichAuthor Commented:
Thanks.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now