Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

How do I validate a date?

( #14685=categorized question: print w/ replies, xml ) Need Help??
Contributed by Anonymous Monk on May 25, 2000 at 02:35 UTC
Q&A  > dates and times


I've created a form that has drop downs for the date. like this:

<select name="day" size="1"> <option value="1"> 1</option> <option value="2"> 2</option> <option value="3"> 3</option> .. <option value="31">31</option> </select> <select name="month" size="1"> <option value="1"> 1</option> .. <option value="12">12</option> </select> <select name="year" size="1"> <option value="2000">2000</option> <option value="2001">2001</option> </select>

then in a perlscript I go:

my $post = param('day') . "-" . param('month') . "-" . param('year');
to use in a query against a DB.

Is there a function in perl to make sure someone doesn't choose 31-FEB-2000, or 29-FEB-1999?

Answer: How do I validate a date?
contributed by plaid

There are a couple ways to do this. The best that comes to mind is to grab a module for it (I prefer Date::Calc personally). You could then do something like

unless(check_date(param('year'), param('month'), param('day')) { # do something }
A non-modular way to accomplish this would be to create an array of month lengths, i.e.
my @month_lengths = (31, 28, 31, 30, ......);
which would require one additional check for leap-year.
Answer: How do I validate a date?
contributed by ZZamboni

You could use the ParseDate subroutine from Date::Manip (or some other date manipulation package) to parse the date. If it parses correctly, it must be valid.

But Date::Manip could be overkill if all you want to do is validate the date. You could just check the day of the month against an array indexed by month in which you store how many days each month has. You would have to pre-check for leap years and modify the number of days in february accordingly.

Leap-year code posted by Adam -- Q&AEditors

# $year is a leap year if ($year % 4 == 0) and ($year % 100 != 0 or $year % 400 == 0)
Answer: How do I validate a date?
contributed by echo

use Date::Calc (); sub valid_date($$$) { my ($day, $month, $year) = @_; return Date::Calc::check_date($year, $month, $day); }
Answer: How do I validate a date?
contributed by I0

use POSIX; $validdate = (gmtime mktime 0,0,0,$day,$mon-1,$year-1900)[4] == $mon-1 +;
Answer: How do I validate a date?
contributed by frankus

I wrote this today for a problem I've got, I'm sure the cook book says something similar..HTH :)

sub valid_date($$$){ my ($day,$month,$year)=@_; $month--; my $epoch=timelocal(0,0,0,$day,$month,$year); my $vdate=localtime($epoch); my @compdate=($vdate->mday,$vdate->mon+1,$vdate->year+1900); for (0..2){print ; if($compdate[$_]!=$_[$_]){ return 0}} 1; }
Answer: How do I validate a date?
contributed by BaldPenguin

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; }

Please (register and) log in if you wish to add an answer

  • 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, 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, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (15)
    As of 2016-02-12 08:49 GMT
    Find Nodes?
      Voting Booth?

      How many photographs, souvenirs, artworks, trophies or other decorative objects are displayed in your home?

      Results (391 votes), past polls