Avatar of Tolgar
Tolgar
 asked on

Hoe to get current time and date in Perl

Hi,
I can get the current date by using the following code:

use POSIX qw(strftime);
my $reportFile = strftime "%Y%m%d", localtime;

Open in new window


This gives me something like this:
20120710

Open in new window


I would like to add the time to this result like this:

20120710_1420

Open in new window


How can I do it in Perl?
Perl

Avatar of undefined
Last Comment
farzanj

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
farzanj

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Tolgar

ASKER
It returns this:

201262_1442

Open in new window


But I was expecting this:

20120710_1442

Open in new window


How can I fix it?
farzanj

Second one works fine.
farzanj

Here's the correct version for #1

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
my $time =  sprintf("%d%02d%02d_%d%d", $year+1900, ++$mon , $mday, $hour, $min);
print $time;

Open in new window

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Tolgar

ASKER
Thanks a lot...
farzanj

Welcome :)