Perl Date::Manip for date validation

When validating dates, sometimes it's easiest to just let people type whatever they want and have your code try to make sense of it. Of course if you can constrain people to specific formats, that's even easier, but that's not always an option - your input may be coming from multiple sources where different rules or expectations are in effect.

Date::Manip is one of the most forgiving and accepting Perl modules. You can throw just about anything at it and get back a formatted date.

Before you rush to CPAN to get this, you might want to know that this module does a LOT of tests. I really recommend using "notest install Date::Manip" with cpan - if you don't, you'll be watching tests for quite a while..

Once you have it, it's simple to use. The script that follows just loops on input and reformats whatever you type into mm/dd/yy format. It also makes sure that you've typed something in the future (just to demonstrate how to convert to Epoch seconds).

use Date::Manip qw(ParseDate UnixDate );
print "Epoch seconds $x\n";
while (<>) {
  print "ooops - don't understand!\n" if not $date;
  next if not $date;
  ($y,$m,$d)=UnixDate($date, "%Y", "%m", "%d");
  ($e)=UnixDate($date, "%o");
  if ($e < $x) {
   print "Can't accept $m/$d/$y\n";
  print "Formatted $m/$d/$y\n";

Date::Manip is not lightning fast but it will swallow just about anything you can imagine - even the output of command line date, even holidays like "Xmas" if you define them.

I like that it has no dependencies - it won't break if some yahoo changes their super-duper module next year. There are apparently plenty of people who despise this code, but look at this (running the code above):

Epoch seconds 1306181152
Can't accept 05/23/2011
Formatted 05/24/2011
next week
Formatted 05/30/2011
next tuesday
Formatted 05/24/2011
next monday
Formatted 05/30/2011
May 15th
Can't accept 05/15/2011
Jun 23rd
Formatted 06/23/2011
Formatted 09/08/2011
Formatted 06/23/2011
Formatted 02/23/2013

See Date::Manip examples for more.


If your input stream needs that kind of flexibility, Date::Manip provides it. If not, the smaller and simpler Date::Calc will do the easy conversions:

while (<>) {
use Date::Calc qw(Decode_Date_US);
print "$y $m $d\n";

That code will not like "2011-09-15" (the Date::Manip version will recognize that) or any of "today", "next week" and so on. It will take "09-15-2011" or "09/15/2011".

Got something to add? Send me email.

(OLDER) <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> Validating free-form input dates with Perl's Date::Manip

Increase ad revenue 50-250% with Ezoic

More Articles by

Find me on Google+

© Anthony Lawrence

Kerio Samepage

Have you tried Searching this site?

Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us

Additional Info

Unfortunately, people are not rebelling against Microsoft. They don’t know any better. (Steve Jobs)

This post tagged: