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

Hi, in the following code I want to display the date in the format yyyy-mm-dd not in the format yyyymmdd.Can you help me?
sub local_yyyymmdd { my $time = shift || time(); my ($year, $month, $mday) = (localtime($time))[5,4,3]; return 10_000*($year + 1900) + 100*($month + 1) + $mday; }
Thank you for your time.

Replies are listed 'Best First'.
Re: displaying the current date
by Corion (Patriarch) on Dec 08, 2003 at 08:13 UTC

    The POSIX module also provides the strftime() function, which is very convenient to use in this case as well:

    use strict; use POSIX qw(strftime); sub local_yyyymmdd { my ($time) = shift || time; return strftime( '%Y-%m-%d', localtime($time)); };

    Documentation on strftime is pretty scarce, as it is the C function of the same name, but the DateTime module has a similar function which takes similar parameters in the format string.

    Updated: Added dashes thanks to ysth, added paragraph about the documentation

    perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web

      DateTime also has a routine ymd that outputs the date in exactly the format he needs :-)

Re: displaying the current date
by ysth (Canon) on Dec 08, 2003 at 08:07 UTC
    sprintf "%.4d-%.2d-%.2d", $year+1900, $month+1, $mday
Re: displaying the current date
by atcroft (Abbot) on Dec 08, 2003 at 08:12 UTC

    You might want to look at the localtime() documentation to get an idea. It also shows an example of using the POSIX module for doing time.

    Recall that year is the difference from 1900, so years 2000 and beyond are 100+, rather than being 2-digit numbers. Just add back the 1900 and you should be okay, I would think. Something along the lines of the following might work:

    sub local_yyyymmdd { my $time = shift || time(); my ($year, $month, $mday) = (localtime($time))[5,4,3]; $year += 1900; $month++; return sprintf "%04d-%02d-%02d", $year, $month, $mday; }

    HTH.

    Update: (08-Dec-2003) Changed format for sprintf from "%4d-%2d-%2d" to "%04d-%02d-%02d" to force leading zeros. (Thank you, ysth, for noting that.)

Re: displaying the current date
by Roger (Parson) on Dec 08, 2003 at 08:11 UTC
    I would do something like below ...
    sub local_yyyymmdd { my $time = shift || time(); my ($year, $month, $mday) = (localtime $time)[5,4,3]; return sprintf "%04d-%02d-%02d", $year+1900, $month+1, $mday; }
Re: displaying the current date
by Steve_p (Priest) on Dec 08, 2003 at 15:07 UTC
    use POSIX qw/strftime/; sub local_yyyymmdd { return strftime "%Y-%m-%d", localtime($_ || time()); }
Re: displaying the current date
by jweed (Chaplain) on Dec 08, 2003 at 08:17 UTC
    Surely:
    ... return sprintf "%04d-%02d-%02d", $year+1900,$month+1,$mday
    *sigh*. I'm late, sorry.


    Who is Kayser Söze?
    Code is (almost) always untested. Your mileage may vary.