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

How do I use the localtime function to get the date but in mm-dd-yyyy format?

Replies are listed 'Best First'.
Re: Date format
by arturo (Vicar) on Mar 19, 2001 at 20:29 UTC

    If you call localtime in list context, it returns an array; what you want are the fourth through sixth elements of that array. A really terse way to nab these three parts is with the following (which uses an array slice):

    my ($day, $month, $year) = (localtime)[3..5];

    The first element of the resulting array (the fourth element returned by localtime is the day, the second is the month (numbered from 0-11) and the third is the number of years since 1900.

    $year +=1900; my $datestr = join "-", ++$month, $day, $year;

    That will get you most of the way (neither month, nor day is guaranteed to be two digits, but there are lots of ways to 'pad' out the strings.)

    You can also get a nice timestamp by calling localtime in a scalar context, which can be forced using the scalar operator (e.g. my $timestamp = scalar localtime;.)

    All this and more is documented in the localtime manpage ...

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

(tye)Re: Date format
by tye (Sage) on Mar 19, 2001 at 21:11 UTC

    Please don't. mm-dd-yyyy is probably not going to be easily understood outside of the United States of America (where dd-mm-yyyy is much more common). However, yyyy-mm-dd

    • is understood around the world (even in the USA, people know what you mean right away)
    • has been formally standardized internationally
    • leads to trivial sorting by date
    And a special thanks to you for not using a 2-digit year. (:

            - tye (but my friends call me "Tye")
      I understand your concern. However, it is only going to be used within the US.
        Are you ever going to sort it?

        Are you ever going to have someone who (like me) grew up in a different country using it?

        Sometimes you have to do what the customer wants, but I strongly second tye's advice. Even if you think this will only be used in the US, if you can you want to use YYYY-MM-DD format.

        There are parts of the US in which d/m/y is more common, as well. In Florida, in particular, (although I'd expect Texas, Arizona, California, Lousiana, and probably Vermont and the Dakotas, if my ethnic stereotyping classes were correct) this is a constant problem. If M/D/Y were so popular, the IRS wouldn't have to print "month" and "day" on their forms.

        Besides which, almost all US institutions of any size -- federal government, military, large corporations, hospitals, &c. -- universally use y/m/d for their short form. It's even becoming standard on movie posters, and I've seen more than one 2001 wall calendar printed that way.

        IMHO, the only place in which it isn't just plain rude to use M/D/Y is if it's plainly labelled as such, or when the month (or possibly even the day) are written out: eg. January 01, 2001, or January the First, 2001.

Re: Date format
by davorg (Chancellor) on Mar 19, 2001 at 20:41 UTC

    Easiest way is probably to use localtime in conjunction with POSIX::strftime as I've just demonstrated here.

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

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

      And to my incredible surprise, the ActiveState Perl port supports Perl's POSIX module even on Windows! It says here that:
      SUPPORTED PLATFORMS
      • Linux
      • Solaris
      • Windows
Re: Date format
by esolm (Acolyte) on Mar 19, 2001 at 20:58 UTC
    The localtime function gets stored in a list

    The 4th element in the list is month 0 for jan and 11 for dec
    The 3rd is the day of the month
    The 5th is the number of years since 1900.

    in this code I add one to the month element and 1900 to the year element this gets current year in YYYY format and gets the month to the correct position.

    I then concatenate the variables together

    Hope this helps
    #!/usr/bin/perl # file name here use warnings; use strict; my @timelist = localtime; $timelist[4] = $timelist[4]+1; $timelist[5] = $timelist[5]+1900; my $target = $timelist[4]."-".$timelist[3]."-".$timelist[5]; print $target, "\n";
Re: Date format
by lachoy (Parson) on Mar 19, 2001 at 22:22 UTC

    Maybe it's a cannon to kill a fly (or has some problems I don't know about), but I've always used the pure-perl Date::Format module without any problems. The following should print out the recommended yyyy-mm-dd format:

    use Date::Format; print time2str( '%Y-%m-%e', time );

    or from the command-line:

    perl -MDate::Format -e 'print time2str( "%Y-%m-%e", time )'

    Chris
    M-x auto-bs-mode

Re: Date format
by bjelli (Pilgrim) on Mar 19, 2001 at 21:22 UTC

    Just my 0.2 XP's worth:

    sub date { my($sec,$min,$h,$mday,$month,$year) = localtime(time); $year += 1900; # 2-digit year (eg. 101 = 2001) $month += 1; # month is counted from 0 return sprintf "%02d-%02d-%04d", $month, $mday, $year; }

    PS: if your audience is international: don't use that format. I'm from Austria, I'm used to interpreting xx-xx-xxxx as DD-MM-YYYY. You might want to use YYYY-MM-DD instead, to avoid this confusion.

    --
    Brigitte    'I never met a chocolate I didnt like'    Jellinek
    http://www.horus.com/~bjelli/         http://perlwelt.horus.at