Rudra
Rudra

Reputation: 739

Copying the date in datetimeformat to a variable in perl

How to copy the current date in datetime format to a variable in perl ?

Suppose current date is 2013-05-30 then the value copied to variable

$time='2013-05-30 15:10:23' 

How can I do this in perl ?

Upvotes: 0

Views: 2347

Answers (1)

choroba
choroba

Reputation: 242113

Use localtime and sprintf:

my ($sec, $min, $hour, $mday, $mon, $year) = localtime;
my $time = sprintf '%d-%02d-%02d %02d:%02d:%02d', 1900+$year, 1+$mon, $mday, $hour, $min, $sec;

Or, using Time::Piece (core since 5.9.5):

use Time::Piece;
my $time = localtime->strftime('%Y-%m-%d %H:%M:%S');

Upvotes: 1

Related Questions