Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

how to convert date from '20100302165019' to '02-MAR-10'?

Replies are listed 'Best First'.
Re: Date conversion in perl?
by ikegami (Patriarch) on Mar 03, 2010 at 05:04 UTC

    unpack and POSIX's strftime can do the trick.

    use POSIX qw( strftime ); my ($y,$m,$d) = unpack( 'A4 A2 A2', '20100302165019' ); print( uc( strftime( "%d-%b-%y", 0,0,0, $d,$m-1,$y ) ), "\n" );

    Well, %b isn't guaranteed to return the months in English, so you might want to create a lookup table instead of using strftime.

    my @months = qw( JAN FEB MAR ... DEC ); my ($y,$m,$d) = unpack( 'A4 A2 A2', '20100302165019' ); print( sprintf( "%02d-%s-%02d", $d, $months[$m-1], $y-2000 ), "\n" );

    print and sprintf can be combined using printf.

    Update: Added lookup table solution.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Date conversion in perl?
by ambrus (Abbot) on Mar 03, 2010 at 13:44 UTC
    perl -we 'use Date::Manip; print uc UnixDate($ARGV[0], "%d-%b-%y\n");' + '20100302165019'
Re: Date conversion in perl?
by jdrago999 (Pilgrim) on Mar 03, 2010 at 18:03 UTC

    How about:

    use HTTP::Date qw( time2iso str2time ); my $original = '20100302165019'; my ($year, $month, $day) = $original =~ m{^(\d{4})(\d{2})(\d{2})}; my $time = str2time("$year\-$month\-$day"); my $localtime = localtime( $time ); my $isodate = time2iso($time); print "Localtime: '$localtime'\n"; print "ISO Date: '$isodate'\n";

    Your output would be:

    Localtime: 'Tue Mar 2 00:00:00 2010' ISO Date: '2010-03-02 00:00:00'