Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.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中从DateTime获取两位数的月份和日期_Perl_Datetime - Fatal编程技术网

在perl中从DateTime获取两位数的月份和日期

在perl中从DateTime获取两位数的月份和日期,perl,datetime,Perl,Datetime,您好,我正在使用Perl的DateTime解析字符串并将其转换为日期格式 我的代码如下所示: my $parser = DateTime::Format::Strptime->new( pattern => "%Y-%m-%dT%H:%M:%SZ", time_zone => 'UTC', on_error => 'croak'); my $startDate = $parser->parse_datetime("2017-04-14T21:00:00Z"); m

您好,我正在使用Perl的DateTime解析字符串并将其转换为日期格式

我的代码如下所示:

my $parser = DateTime::Format::Strptime->new( pattern => "%Y-%m-%dT%H:%M:%SZ", time_zone => 'UTC', on_error  => 'croak');
my $startDate = $parser->parse_datetime("2017-04-14T21:00:00Z");
my $endDate = $parser->parse_datetime("2017-04-14T23:00:00Z");
print $startDate->month();
当我打印月份时,我只得到4,或者如果日期更改为小于10的数字,我得到一个单位数字

如果数字小于10,是否有更干净的方法获得两位数的月份/日期而不在数字前加零

使用sprintf(或者printf,如果您只是想打印它的话)


它会将你的输入归零。查看以了解有关format参数的更多信息。

您也可以获得所有这些参数,因为您可能需要它们

my ($year, $mon, $day) = unpack "A4A2A2", $startDate->ymd('');

默认的分隔符为
-
(破折号)

my ($year, $mon, $day) = unpack "A4A2A2", $startDate->ymd('');
my ($year, $mon, $day) = split /-/, $startDate->ymd();