Link to home
Start Free TrialLog in
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?
ASKER CERTIFIED SOLUTION
Avatar of farzanj
farzanj
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tolgar
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?
Second one works fine.
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

Avatar of Tolgar

ASKER

Thanks a lot...
Welcome :)