in reply to Convert US dates to mySQL dates

Well, I don't know exactly what you are looking to do to "validate date", but the conversion is pretty straight forward:

my $date = "12-02-2000"; (my $out = $date) =~ s/(\d{1,2})-(\d{1,2})-(\d{2,4})/$3\-$1\-$2/;

Printing $out returns 2000-12-02. Sure, you can shorten it some, or use different syntax, but this should handle most of these date conversions.

Of course, the standard "use strict" and "-w" recommendations apply.

Replies are listed 'Best First'.
Re: Re: Convert US dates to mySQL dates
by diarmuid (Beadle) on May 01, 2001 at 15:44 UTC
    Well I would do it like the above and I guess the validation could be included also.
    $date="24-02-2000"; if ($date =~ /(\d{2})-(\d{2})-(\d{4})/){ $month=$1;$day=$2,$year=$3; print "ERROR month invalid",$month="01","\n" if($1 > 12); print "ERROR day invalid",$day="01","\n" if($2 > 31); print "ERROR year invalid",$year="2000","\n" if($3 > 2100 || $3 < 19 +00); $new_date = $year."-".$month."-".$day; } print "$new_date\n";
    You could be as fancy with the validation as you want.
    tidy up the code as much as you desire