Today's Date in Perl in MM/DD/YYYY format


I'm working on a Perl program at work and stuck on (what I think is) a trivial problem. I simply need to build a string in the format '06/13/2012' (always 10 characters, so 0's for numbers less than 10).

Here's what I have so far:

use Time::localtime;
my ($day,$month,$year)=($tm->mday,$tm->month,$tm->year);

This question is tagged with perl date formatting

~ Asked on 2012-06-13 18:09:30

The Best Answer is


You can do it fast, only using one POSIX function. If you have bunch of tasks with dates, see the module DateTime.

use POSIX qw(strftime);

my $date = strftime "%m/%d/%Y", localtime;
print $date;

~ Answered on 2012-06-13 18:13:22


You can use Time::Piece, which shouldn't need installing as it is a core module and has been distributed with Perl 5 since version 10.

use Time::Piece;

my $date = localtime->strftime('%m/%d/%Y');
print $date;




You may prefer to use the dmy method, which takes a single parameter which is the separator to be used between the fields of the result, and avoids having to specify a full date/time format

my $date = localtime->dmy('/');

This produces an identical result to that of my original solution

~ Answered on 2012-06-13 18:14:44

Most Viewed Questions: