在Perl中解析长日期格式

问题描述 投票:0回答:1

是否有perl模块将这种日期格式解析为DateTime对象?

2007年9月13日,星期四

我尝试过DateTime::Format::Natural,但它似乎没有用。

例如,如果没有一周的某一天,我更喜欢一种不介意的功能。我无法确定数据是否一致。

perl date datetime
1个回答
2
投票

这可以使用DateTime::Format::Flexible来实现。

use DateTime::Format::Flexible qw( );

my $dt = DateTime::Format::Flexible->parse_datetime(
   'Thursday, September 13, 2007',
   lang => [ 'en' ],                    # Optional
);

如果您已经知道具体的格式(如原始问题),我会推荐DateTime::Format::Strptime

use DateTime::Format::Strptime qw( );

my $format = DateTime::Format::Strptime->new(
   pattern  => '%A, %B %e, %Y',
   locale   => 'en',
   on_error => 'croak',
);

my $dt = $format->parse_datetime('Thursday, September 13, 2007');
© www.soinside.com 2019 - 2024. All rights reserved.