CJ7
CJ7

Reputation: 23295

Parse long date format in Perl

Is there a module for perl that will parse this format of date into a DateTime object?

Thursday, September 13, 2007

I have tried DateTime::Format::Natural but it doesn't seem to work.

I would prefer a function that doesn't mind if there isn't a day of the week, for instance. I can't be sure if the data will be consistent.

Upvotes: 0

Views: 127

Answers (1)

ikegami
ikegami

Reputation: 386706

This can be achieved using DateTime::Format::Flexible.

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

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

If you had known the specific format (as in your original question), I would have recommended 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');

Upvotes: 3

Related Questions