Reputation: 5247
I am trying to check whether a variable $test
contains a valid date format by attempting to parse it with Date::Manip. It works as expected when the format is 'YYYY/MM/DD'. If I change the format to 'DD/MM/YYYY' (my $test="20/4/2012") it's not working properly. I tried a config function for setting to uk date format but still it didn't work.
use Date::Manip qw(ParseDate );
my $test="20_4_2012";
#my $test="2012_4_20";
$test =~ s/_/\//g;
print $test;
$date=ParseDate($test);
if(!$date) {
print "baddate : $date\n";
}
else {
print "Guddate: $date\n";
}
Upvotes: 2
Views: 2703
Reputation: 1763
Hopefully you've already found your solution :-)
But for the benefit of anyone passing through looking for some date parsing tools for Perl, I can highly recommend the core module Time::Piece
-- it's insanely good at working out what you meant from any old date string.
The real answer to your specific question is "it depends what you mean by valid" of course.
Upvotes: 1
Reputation: 14741
What did your config function call look like? I can get your script to work by adding this:
Date_Init("DateFormat=non-US");
before the ParseDate. (And also adding Date_Init
to the import list in the use
)
Upvotes: 1
Reputation: 32534
You can do this using the Time::ParseDate
which allows you to specify UK
in it's arguments to the parser function.
use Time::ParseDate;
my $test="20/4/2012";
$time = parsedate($test, UK => 1);
Upvotes: 1