in reply to Date String Conversion?

You can easily do it yourself:

use strict; use warnings; my $str = shift @ARGV; my %months = ( 'Jan' => '01', 'Feb' => '02', 'Mar' => '03', 'Apr' => '04', 'May' => '05', 'Jun' => '06', 'Jul' => '07', 'Aug' => '08', 'Sep' => '09', 'Oct' => '10', 'Nov' => '11', 'Dec' => '12' ); my ($year,$month,$day) = (substr ($str,-2,2,""),substr($str,-3,3,""),$ +str); my $modstr = join '-',($months{$month},length $day == 1 ? '0'.$day : $ +day, '20'.$year); print $modstr."\n"; # Run it!: # perl date_convert.pl 16May07 # 05-16-2007 # perl date_convert.pl 4Dec03 # 12-04-2003

But be careful with years < 2000! :-)

citromatik

Replies are listed 'Best First'.
Re^2: Date String Conversion?
by naikonta (Curate) on Jun 08, 2007 at 16:01 UTC
    Save yourself :-)
    #!/usr/bin/perl use strict; use warnings; my %months = ( Jan => 1, Feb => 2, Mar => 3, # ..... ); my $str = shift; my($day, $mon, $year) = split /(\D+)/, $str; printf "%02d-%02d-%s\n", $months{$mon}, $day, "20$year";
    I don't feel good with the 20$year part, though. It reminds me of the 19100 syndrome :-)

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

      By your "Save yourself" I was thinking you were going to build the month name-number hash more lazily...

      my %months; @months{qw( jan feb mar apr may jun jul aug sep oct nov dec )} = ( 1 .. 12 );
        Believe it or not, that was my initial draft in one-liner version. But I then decided to write it in a full script, and followed citromatik's style in hash building with a little modification. Don't get me wrong, I do like slices :-) OTOH, I intentionally focused on the input parsing and output printing parts, where citromatik's version seems to suffer from, in efforts and clarity.

        Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!