Chon-Ji has asked for the wisdom of the Perl Monks concerning the following question:

Hi! How do I format the date that I got from perl's localtime? I need to be as flexible as possible, for example I could change to YYYYMMDD, MMDDYY and so on. I was hoping perl has some sort of function where I just need to specify the pattern and perl will do the formatting for me.

Replies are listed 'Best First'.
Re: Date format
by andreas1234567 (Vicar) on Sep 05, 2007 at 06:41 UTC
Re: Date format
by mr_mischief (Monsignor) on Sep 05, 2007 at 05:02 UTC
    There's the POSIX module (part of the core perl distribution) which has the strftime() function, which is very flexible.

    That said, there are lots of good Date:: and Time:: modules on CPAN.

Re: Date format
by dsheroh (Monsignor) on Sep 05, 2007 at 05:06 UTC
    From Perl Cookbook:
    ($seconds, $minutes, $hours, $day_of_month, $month, $year, $wday, $yda +y, $isdst) = localtime($time); printf("Dateline: %02d:%02d:%02d-%04d/%02d/%02d\n", $hours, $minutes, +$seconds, $year+1900, $month+1, $day_of_month);
    If you don't feel like doing the formatting yourself, there are also... many... date/time-related modules on CPAN.