I wanted to validate a date AND it's format, and wanted to allow any ISO standard formatted date. This may not be the most elegant, but it does work.
use Date::Parse;
use Date::Manip qw();
sub is_valid_date_string {
my $str = shift;
my @elms = strptime($str);
# Date::Parse will leave undefs if the elem cannot be found in the
+ string
return 0 unless defined($elms[3]) && defined($elms[4]) && defined(
+$elms[5]);
my $str_r = sprintf("%4d-%02d-%02d", $elms[5]+1900, $elms[4]+1, $e
+lms[3] );
# Run back trhough Date::Manip to make sure whatever we parsed is
+a valid date
return 0 unless Date::Manip::UnixDate($str_r,"%Y-%m-%d") eq $str_r
+;
return 1;
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.