Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Perl中获取日期时间中的当前时间_Perl_Datetime_Timestamp - Fatal编程技术网

如何在Perl中获取日期时间中的当前时间

如何在Perl中获取日期时间中的当前时间,perl,datetime,timestamp,Perl,Datetime,Timestamp,我正在编写一个Perl脚本,它以datetime格式获取当前时间。如何在Perl中以datetime格式将当前时间存储到变量中?您可以使用“localtime()”返回历元时间 use strict; use warnings; use DateTime; my $dt = DateTime->now; # Stores current date and time as datetime object 您可以将其转换为字符串时间。如果那是你想要的 strftime("%Y

我正在编写一个Perl脚本,它以datetime格式获取当前时间。如何在Perl中以datetime格式将当前时间存储到变量中?

您可以使用“localtime()”返回历元时间

use strict;
use warnings;
use DateTime;    
my $dt   = DateTime->now;   # Stores current date and time as datetime object
您可以将其转换为字符串时间。如果那是你想要的

strftime("%Y-%m-%d %H:%M:%S", localtime);

在标准POSIX模块中使用strftime:

$ perl -MPOSIX -le 'print strftime "%F %T", localtime $^T'

在Perl的绑定中,strftime的参数被设计为与localtimegmtime的返回值对齐