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

O enlightened ones

I would like to rearrange the format of a date from dd/mm/yyyy to yyyymmdd (probably using regular expressions and splitting).

Any ideas?

Thanks in advance

Campbell

Replies are listed 'Best First'.
Re: rearranging dates
by Zaxo (Archbishop) on Jun 19, 2003 at 12:58 UTC

    Here goes, as an exercise call perldoc -f on each keyword: my $new = join '', reverse split /\//, $date;

    This wasn't homework was it?

    After Compline,
    Zaxo

Re: rearranging dates
by davorg (Chancellor) on Jun 19, 2003 at 13:01 UTC
    my $date = 'dd/mm/yyyy'; if (my @date = split /\//, $date) { $date = join '', @date[2, 1, 0]; }
    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: rearranging dates
by vek (Prior) on Jun 19, 2003 at 13:08 UTC

    Just for grins you could also use substr:

    my $DDMMYYYY = '16/06/2003'; my $YYYYMMDD = substr($DDMMYYYY, 6, 4) . substr($DDMMYYYY, 3, 2) . substr($DDMMYYYY, 0, 2);

    -- vek --
Re: rearranging dates
by nite_man (Deacon) on Jun 19, 2003 at 13:18 UTC
    You can do it at this way:
    my $date = '23/06/2003'; $date =~ m!([0-9]{2})/([0-9]{2})/([0-9]{4})!;
    or
    $date =~ m!(\d{2})/(\d{2})/(\d{4})!; print "$3-$2-$1\n"; __DATE__ 2003-06-23

    Updated!

    my $date = '23/06/2003'; $date =~ s!(\d{2})/(\d{2})/(\d{4})!$3-$2-$1!; print $date; __DATE__ 2003-06-23

          
    --------------------------------
    SV* sv_bless(SV* sv, HV* stash);
    
Re: rearranging dates
by ant9000 (Monk) on Jun 19, 2003 at 13:32 UTC
    It's been largely answered, but here's my attempt:
    $_="DDMMYYYY"; print pack("A4A2A2",reverse(unpack("A2A2A4",$_))),$/;
Re: rearranging dates
by hmerrill (Friar) on Jun 19, 2003 at 14:12 UTC
    Here's my shot - slightly different than other options so far:
    my $ddsmmsyyyy = "19/06/2003"; my($dd, $mm, $yyyy) = split /\//, $ddsmmsyyyy; my $yyyymmdd = $yyyy.$mm.$dd;