Zerobu
Zerobu

Reputation: 2701

Matching a date in perl

I want to match a date in the format day/month/year. where day is two digits month is two digits and year is four digits. Also, I want to check see if it is a valid date, for example knows when is leap year, and know which month has 30days, 31days and 28, or 29 days for Februrary.

Upvotes: 2

Views: 655

Answers (2)

muruga
muruga

Reputation: 2122

use the following code

use strict;
use warnings;
use Date::Manip;
my $start="2010:03:30:23:02:3";
my $split=":";
my($year,$month,$date,$hour,$min,$sec);

($year,$month,$date,$hour,$min,$sec)=split($split,$start);

my $result = ParseDate("$month/$date/$year");
if(!$result)
{
    print "Invalid Date\n";
    exit;
}

Upvotes: 1

WhirlWind
WhirlWind

Reputation: 14112

Take a look at something like Date::Manip; there's little sense in doing this yourself when things like this are available.

$date = ParseDate($mydate);
unless ($date) {
  # error
}
...

Upvotes: 4

Related Questions