我想将以下日期转换为MM-DD-YYYY格式。
Sep 12 00:00:00 2012在 Perl 中
对于同时提供strptime()`strftime()' 功能的核心模块,您可以使用Time::Piece。
use Time::Piece;
my $date = q(Sep 12 01:02:03 2012);
my $t = Time::Piece->strptime($date, "%b %d %H:%M:%S %Y");
print $t->strftime("%m-%d-%Y\n");
有几个模块可以做到这一点,但Time::Piece可能是最好的选择,因为它自 v5.9 以来一直是核心 Perl 发行版的一部分。
这段代码可以满足您的要求。请注意,%e格式中strptime预计从1到的天数31。如果日期被零填充到两位数,则%d必须使用它来代替01to 31。
use strict;
use warnings;
use Time::Piece;
my $dt = Time::Piece->strptime('Sep 12 00:00:00 2012', '%b %e %T %Y');
print $dt->strftime('%d-%m-%Y');
输出
12-09-2012
use DateTime::Format::Strptime qw( );
my $input_format = DateTime::Format::Strptime->new(
pattern => '%b %d %H:%M:%S %Y',
locale => 'en_US',
time_zone => 'local',
on_error => 'croak',
);
my $dt = $input_format->parse_datetime('Sep 12 00:00:00 2012');
say $dt->strftime('%m-%d-%Y');