Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Date formatting

by ibanix (Hermit)
on Jun 30, 2003 at 12:49 UTC ( #270150=perlquestion: print w/replies, xml ) Need Help??

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

Hello fellow Monks,

I've been absent from the monstary a long time, with work and such claiming hold of my life. It's appropriate then that I have a fairly simple question to ask on my return.

I have a date field in a file of type 'MM/DD/YYYY' where MM, DD, and YYYY are month, day and year respectively. I need to convert this to 'YYYY-MM-DD'.

I think my obvious approach would be to split the field and rewrite it like so:

$datefield = 'MM/DD/YYYY'; ($mon, $day, $year) = split(/\//,$datefield,3); $finaldate = $year."-".$mon."-".$day;
However, I'd really like to not reinvent well-working wheels -- is there a good date formatting function that will do this? I attempted to make localtime() do what I needed, without immediate success.

Thank you all!
ibanix

$ echo '$0 & $0 &' > foo; chmod a+x foo; foo;

Replies are listed 'Best First'.
Re: Date formatting
by davorg (Chancellor) on Jun 30, 2003 at 13:04 UTC

    No, that sounds like a pretty good approach to me. You can get rid of your intermediate variables tho'.

    $finaldate = join '-', (split(/\//, $datefield, 3))[2,0,1];
    --
    <http://www.dave.org.uk>

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

Re: Date formatting
by dragonchild (Archbishop) on Jun 30, 2003 at 13:10 UTC
    If you're doing more than that, I would suggest looking at Date::Manip and Date::Calc.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      Why bother loading external (and very large and slow) modules when this is really a text processing problem that can be solved very easily with standard Perl functions?

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

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

        "If you're doing more than that" ... where "that" implies your original reply.

        While Date::Manip is large and slow and all things horrible ... Perl is large and slow and all things horrible too! If you're looking for execution speed, go to ASM. If you're looking for development speed, use the appropriate modules. Personally, I don't think the OP wanted the fastest solution. I believe he (and nearly everyone who posts on PM) is looking for the easiest solution. Both solutions are easy. Yours is easier in that it doesn't require learning how to use modules. Date::Manip is easier when the requirements change, like they always do.

        ------
        We are the carpenters and bricklayers of the Information Age.

        Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

        Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      More specifically, with Date::Manip, you can parse and reformat with a single function call:

      $ perl -MDate::Manip -le 'print UnixDate("06/07/2003","%Y-%m-%d");' 2003-06-07

      -xdg

      Code posted by xdg on PerlMonks is public domain. It has no warranties, express or implied. Posted code may not have been tested. Use at your own risk.

Re: Date formatting
by nite_man (Deacon) on Jun 30, 2003 at 16:01 UTC
    Try to do like this:
    my $date = '06/30/2003'; $date =~ s!(\d{2})/(\d{2})/(\d{4})!$3-$1-$2!;
          
    --------------------------------
    SV* sv_bless(SV* sv, HV* stash);
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://270150]
Approved by Corion
Front-paged by hsmyers
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2022-08-10 04:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?