Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Validate date value

by ashokpj (Hermit)
on Oct 04, 2007 at 11:21 UTC ( #642627=perlquestion: print w/replies, xml ) Need Help??
ashokpj has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I like to validate date value, below scripts will find if day is more the 31 days and month is more than 12. But this scripts incorrectly allow for february to have 31 days

please let me know which module support to validate date value(involved leap years ) and format.

use Time::Local; my ($sec,$min,$hour) = qw( 0 0 12 ); my ($mday, $mon, $year) = qw(31 02 2012); $mon--; # because that's how unix/C treat the month value eval { my $dummy = timelocal($sec,$min,$hour,$mday,$mon,$year); }; print $@; if (my $err = $@) { print "This is an invalid date."; } else { print "Yay"; };

Replies are listed 'Best First'.
Re: Validate date value
by Punitha (Priest) on Oct 04, 2007 at 11:42 UTC
    Hi ashok,
    This is the another way to validate the date value.Try this
    use strict; use warnings; use Date::Calc qw(check_date); my ($mday, $mon, $year) = qw(28 02 2012); if (check_date($year,$mon,$mday)){ ####Your stuffs here } else{ print "Check the date is not a valid one\n"; }
Re: Validate date value
by bart (Canon) on Oct 04, 2007 at 11:47 UTC
    You're using timelocal already... good. Now you just have to check, after conversion of $dummy (which is an epoch time in seconds) back to date/time components, with localtime, whether you end up with the same day of the month. If not, it must have wrapped to the next month.
    eval { my $dummy = timelocal($sec,$min,$hour,$mday,$mon,$year); die "Day out of range" unless (localtime($dummy))[3] == $mday; };

    Note that you also got accepted dates for the 31st of all other short months than just February: April, June, September, November. Now they'll all be caught.

Re: Validate date value
by shmem (Chancellor) on Oct 04, 2007 at 11:57 UTC
    In the Tutorials section, you find Getting started with DateTime scrolling around a little bit. DateTime is on CPAN.


    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Validate date value
by svenXY (Deacon) on Oct 04, 2007 at 11:47 UTC
    ... my $dummy; eval { $dummy = timelocal($sec,$min,$hour,$mday,$mon,$year); }; ...
    will output:
    $ perl Day '31' out of range 1..29 at line 13 This is an invalid date.
    You define your $dummy within the eval{}, therefore it's invisible after eval finishes.
Re: Validate date value
by apl (Monsignor) on Oct 04, 2007 at 11:43 UTC
    CPAN is your friend.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://642627]
Approved by svenXY
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2018-06-18 04:50 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (107 votes). Check out past polls.