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

Hi.

I would like to rearrange a date from yyyymmdd to dd/mm/yyyy.

Any ideas about how to do this? (Probably uses something like join '/', reverse split /some sort of pattern/ $date; , but I'm not sure on the pattern)

Thanks,

John

Replies are listed 'Best First'.
Re: rearranging dates
by Zaxo (Archbishop) on May 10, 2004 at 14:28 UTC

    Another way,

    local $_ = '20040515'; my ($year, $month, $day) = unpack 'A4 A2 A2', $_; my $date = "$day/$month/$year";
    pack/unpack are always worth a try with fixed-width data.

    After Compline,
    Zaxo

Re: rearranging dates
by Juerd (Abbot) on May 10, 2004 at 14:21 UTC

    I would like to rearrange a date from yyyymmdd to dd/mm/yyyy.

    If you just see this as a simple string, and forget for a moment that you're dealing with a date, it's easier:

    s[(....)(..)(..)][$3/$2/$1];

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

Re: rearranging dates
by halley (Prior) on May 10, 2004 at 14:17 UTC
    Lots of ideas. Have you tried anything yourself?
    $date =~ s| (\d\d\d\d) (\d\d) (\d\d) |$3/$2/$1|x;

    --
    [ e d @ h a l l e y . c c ]

Re: rearranging dates
by perlinux (Deacon) on May 10, 2004 at 14:38 UTC
    $_="20040510"; s#(\d{4})(\d{2})(\d{2})#$3/$2/$1#; print;