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

I'm currently working on a script to check data polled from several different sources. This script is just a snippet, really, using date::simple , and simply outputs the date before today to a text file, which other scripts will read to make sure I am reading in the correct data.

My question is this. The date format I got using date::simple is 2003-08-01, and I need to convert it to 080103. Looking through previous discussions here, I've seen some in favor, and some against using date::manip for what I need to do. Any help would be very appreciated.

Replies are listed 'Best First'.
Re: Changing format of a date.
by halley (Prior) on Aug 04, 2003 at 15:01 UTC
    It looks like you've got strings of a pretty simple formula, and you want other strings of a pretty simple formula. A single s/// could probably do everything you need. What have you actually tried so far?

    And I know that requirements often come from outside your control, but I would advise against the MMDDYY format and stick with ISO-standard YYYY-MM-DD format. Why erase the century digits? Why use a format that will take a lot of effort to sort properly? Is 0801 supposed to be August 1 or 8 January? The ISO format takes a few more characters but gives back more in terms of readability, stability and functionality.

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

      The reason im switching to mmddyy from yyyy-mm-dd format is due to the proprietary database system i use at work, it is quite old and set in its ways, much like everything else here. I can assure you I'd love to change things up, but in my odd position, I'm given all the responsiblity in the world, and zero power or authority. I gotta basically limbo here at work, under BS deadlines, etc. and not being able to change things.
        Nobody has mentioned Time::Piece which has a nice interface to strptime and strftime:
        use Time::Piece; print localtime ->strptime('2003-08-01','%Y-%m-%d') ->strftime('%m%d%y') ;

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)
        
Re: Changing format of a date.
by vek (Prior) on Aug 04, 2003 at 15:02 UTC

    Is the date always going to be YYYY-MM-DD? If so, you could just use substr to convert it yourself

    #!/usr/bin/perl -w my $oldDate = '2003-08-01'; my $YY = substr($oldDate, 2, 2); my $MM = substr($oldDate, 5, 2); my $DD = substr($oldDate, 8, 2); my $newDate = $MM . $DD . $YY;
    -- vek --
Re: Changing format of a date.
by davorg (Chancellor) on Aug 04, 2003 at 15:46 UTC

    There's very little that Date::Manip can do that other modules can't do better and faster.

    In your case, the problem is so simple that an external module isn't needed.

    $date = '2003-08-01'; $newdate = join('', ($date =~ /(\d\d)/g)[2, 3, 1]);

    But as others have pointed out, please do all you can to resist moving from the logical and useful YYYY-MM-DD format to the illogical and useless MMDDYY.

    --
    <http://www.dave.org.uk>

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

Re: Changing format of a date.
by fletcher_the_dog (Friar) on Aug 04, 2003 at 15:50 UTC
    You could use a simple substitution like this:
    s/^\d\d(\d\d)-(\d\d)-(\d\d)$/$2$3$1/ or die "Improperly formatted date + $_\n";
    But I have to agree with halley that it is better to keep it in the YEAR-MONTH-DAY format for sorting and ambiguity reasons.
Re: Changing format of a date.
by djbiv (Scribe) on Aug 04, 2003 at 18:54 UTC
    here is my preference: you can modify however you like, but I agree with keeping the century as well...
    my($sec,$min,$hour,$mday,$mon,$year) = localtime; my $current_time = sprintf("%02d%02d%02d%02d%02d%02d", $year-100,$mon+ +1,$mday,$hour,$min,$sec);
    it's output of the time is in this format "030804135240"
      I'm ultimately going with your idea there, after coming back to this problem. Thanks!