将datetimeformat中的日期复制到perl中的变量

将datetimeformat中的日期复制到perl中的变量,perl,datetime,Perl,Datetime,如何将datetime格式的当前日期复制到perl中的变量 假设当前日期为2013年5月30日,则值复制到变量 $time='2013-05-30 15:10:23' 如何在perl中做到这一点?使用localtime和sprintf: my ($sec, $min, $hour, $mday, $mon, $year) = localtime; my $time = sprintf '%d-%02d-%02d %02d:%02d:%02d', 1900+$year, 1+$mon, $md

如何将datetime格式的当前日期复制到perl中的变量

假设当前日期为2013年5月30日,则值复制到变量

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

如何在perl中做到这一点?

使用
localtime
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;
或者,使用
Time::Piece
(从5.9.5开始的核心版本):


是的…这是有效的…但是有没有更简短的内容可以包含在code@Vishu:请参阅第二个选项。@Vishu
my$date=localtime
将为您提供一个时间戳,但不是您想要的格式。
use Time::Piece;
my $time = localtime->strftime('%Y-%m-%d %H:%M:%S');