How to check if date is valid in perl?

dmalovich
dmalovich used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Try
use DateTime::Format::ISO8601;
eval {
    $date = DateTime::Format::ISO8601->parse_datetime("20061730");
};
if ( $@ ) { print "ERROR: date not valid" };

Open in new window

Author

Commented:
What does "20061730" represent?
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

Author

Commented:
Thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial