tags:

views:

74

answers:

2

Hello, 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.

+4  A: 

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
}
...
WhirlWind
thanks, it seemed to work, for month/day/year formats but i am having trouble getting it to work for day/month/year formats.
Zerobu
@Zerobu 10/12/2010 is ambiguous. Date::Manip assumes American style, October 12th. You have to tell it otherwise. See DateFormat in http://search.cpan.org/~sbeck/Date-Manip-5.56/lib/Date/Manip.pod#DATE::MANIP_VARIABLES for details.
Schwern
+1  A: 

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