localtime format as "1999-01-28"

Hi.......
I am using ....
$DATE = (join'-',(split/ /,localtime)[4,1,2]);
this returns "1999-Jan-28", but I need the Month to be a digit. "1999-01-28".
regards
LVL 3
rickyrAsked:
Who is Participating?
 
ozoCommented:
answer
0
 
martinagCommented:
@time = localtime;
$DATE = sprintf "%d-%02d-%02d", $time[5]+1900, $time[4]+1, $time[3];

Martin
0
 
martinagCommented:
Or to make it a one-liner...
$DATE = sprintf "%d-%02d-%02d", (localtime)[5]+1900, (localtime)[4]+1, (localtime)[3];

Martin
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
ozoCommented:
#also:
use POSIX;
$DATE = strftime("%Y-%m-%d",localtime);
0
 
ozoCommented:
#another one-liner:
$DATE = sprintf"%d-%02d-%02d",map{$_+(0,1,1900)[-(?^?..!??)]}(localtime)[5,4,3];
0
 
rickyrAuthor Commented:
hi....
I've been using the POSIX one, mainly coz I wanted to add %H-%M-%S for the tme aswell.
thanx, please answer.
0
 
ozoCommented:
$time=sprintf"%02d-%02d-%02d",(localtime)[2,1,0];
#or
use POSIX;
$time=strftime("%H-%M-%S",localtime);
#or
$time = join'-',(split/\W/,localtime)[3..5];
0
 
rickyrAuthor Commented:
Thanx ozo, but I meant re-post as an answer.

0
 
rickyrAuthor Commented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.