2

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.

Zerobu
  • 2,561
  • 7
  • 25
  • 32
  • 1
    Also see http://stackoverflow.com/questions/411740/how-can-i-parse-dates-and-convert-time-zones-in-perl – FMc Apr 04 '10 at 04:03

2 Answers2

4

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
}
...
cjm
  • 61,471
  • 9
  • 126
  • 175
WhirlWind
  • 13,974
  • 3
  • 42
  • 42
  • 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 Apr 04 '10 at 04:29
  • 4
    @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 Apr 04 '10 at 05:31
1

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
  • 2,092
  • 2
  • 20
  • 28