Perl 尝试使用'时出错;strftime';按时

Perl 尝试使用'时出错;strftime';按时,perl,datetime,strftime,Perl,Datetime,Strftime,我正在尝试对数据字符串使用strftime: my $date = '2013-10-31 13:55'; my $new_date = $date->strftime('%Y-%m-%d %H:%M:%S'); 但我总是犯错误 "Can't call method "strftime" without a package or object reference" 请建议我能做些什么将日期从一种格式转换为另一种格式,通常将原始表示形式解析为其部分,然后将其格式化为所需的表示形式。但是

我正在尝试对数据字符串使用strftime:

my $date = '2013-10-31 13:55';
my $new_date = $date->strftime('%Y-%m-%d %H:%M:%S');
但我总是犯错误

 "Can't call method "strftime" without a package or object reference"

请建议我能做些什么

将日期从一种格式转换为另一种格式,通常将原始表示形式解析为其部分,然后将其格式化为所需的表示形式。但是,本例中缺少的只是秒数,因此您只需要以下内容:

 my $new_date = $date . ':00';

在您的示例中,
$date
是标量,而不是对象。它没有一个方法
strftime
(因此错误是没有包或对象引用)。同样,如果您只想在字符串中添加秒数,请使用
操作符将其追加。不要用大锤敲开螺母

如果您正在对格式做一些不那么琐碎的事情,您可以使用和执行以下操作:


正如其他人所指出的,您尝试在此处使用
strftime()
可能会使这一点过于复杂。如果您试图对不包含对象的变量调用方法,您也会产生误解

我想也许你是想写这样的东西

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;
use Time::Piece;

# The original date as a string
my $date_str = '2013-10-31 13:55';
# Parse the date string and create a Time::Piece object
my $date     = Time::Piece->strptime($date_str, '%Y-%m-%d %H:%M');
# Create a new (improved!) date string using strftime()
my $new_date = $date->strftime('%Y-%m-%d %H:%M:%S');
say $new_date;

多年来一直是标准的Perl库。

您想做什么?此日期和时间将显示在页面/portlet上。就这样。我只需要这个格式
#!/usr/bin/perl

use strict;
use warnings;
use 5.010;
use Time::Piece;

# The original date as a string
my $date_str = '2013-10-31 13:55';
# Parse the date string and create a Time::Piece object
my $date     = Time::Piece->strptime($date_str, '%Y-%m-%d %H:%M');
# Create a new (improved!) date string using strftime()
my $new_date = $date->strftime('%Y-%m-%d %H:%M:%S');
say $new_date;