Perl, a subroutine that takes in a date string of various format and return date string in format yyyy-mm-dd

Need a subroutine that takes in a date string of various format and return date string in format yyyy-mm-dd.  If date is not a valid date then rerun 0;  I would prefer if you use any of the Perl date library.
rgbcofAsked:
Who is Participating?
 
ozoConnect With a Mentor Commented:
use POSIX;
sub yyyymmdd{
  return 0 unless my($m,$d,$y) = shift =~ m((\d+)/(\d+)/(\d+));
  $y += 2000 if $y < 100;
  return 0 unless (localtime mktime 0,0,12,$d,$m-1,$y-1900)[4] == $m-1;
  return sprintf"%04d-%02d-%02d",$y,$m,$d;
}
0
 
ozoCommented:
use Date::Parse;
use POSIX;
sub yyyymmdd{
 my @F;
 return strftime "%Y-%m-%d",@F if @F=strptime shift;
 return 0;
}
0
 
rgbcofAuthor Commented:
Is there another way?  My system does not have Date::Parse, and I am not allowed to include the library in the system.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
ozoCommented:
If you want to write your own date parser, can you list all the various date formats that you want to convert?
0
 
rgbcofAuthor Commented:
Acceptable input format:
01/02/2010
1/2/2010
01/02/10
1/2/10

Output format:
yyyy-mm-dd

Also, the date has to be a valid date (13/13/2010 would be invalid)
0
 
rgbcofAuthor Commented:
Ozo, you are such a genius.  

Thank you so much.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.