Perl:简单的方法将MM / DD / YYYY重新排列为YYYY-MM-DD?

时间:2011-06-20 19:02:28

标签: perl

示例输入:8/23/2008(单个数字日/月不会得到前导零)

期望的输出:2008-08-23

喜欢使用perl。谢谢!

4 个答案:

答案 0 :(得分:7)

使用DateTime::Format::StrptimeDateTime(如果他们必须以任何方式处理日期或时间,那么每个人都应该安装):

my $parser   = DateTime::Format::Strptime->new(pattern => '%m/%d/%Y');
my $iso_date = $parser->parse_datetime('8/23/2008')->ymd('-');

答案 1 :(得分:4)

sprintf '%3$04d-%02d-%02d', split m:/:, $inputDate

答案 2 :(得分:2)

如果您尚未安装DateTime,则可以使用Time :: Piece:

use Time::Piece;

my $t = Time::Piece->strptime('8/23/2008', '%m/%d/%Y');
print $t->strftime('%Y-%m-%d'),"\n";

答案 3 :(得分:-1)

join '-', (split qr{/}, $_)[2,0,1]
相关问题