菜鸟问下11/Apr/2016:00:00:01怎么转化成2016-04-11 00:00:01

perl模块推荐_DateTime::Format::Strptime时间的格式化

摘要

之前推荐过DateTime,它的特点是时间的计算。DateTime::Format::Strptime的特点可以设置时间的输入格式和输出格式。

问题

xx问下11/Apr/2016:00:00:01怎么转化成2016-04-11 00:00:01?

demo
#!/usr/bin/perl -w 
 
use strict;
 
use DateTime::Format::Strptime;
my $inputformatter = DateTime::Format::Strptime
                    ->new( pattern   => '%d/%b/%Y:%H:%M:%S',
                           on_error  => 'croak',);                                                                    
my $string='11/Apr/2016:00:00:01';
my $dt=$inputformatter->parse_datetime($string);
my $outputformatter=DateTime::Format::Strptime
                      ->new( pattern => '%Y-%m-%d %H:%M:%S',
                             on_error  => 'croak',);
print $outputformatter->format_datetime($dt);
格式讲解

基本元素

综合元素