How to parse a string into a DateTime object in Perl?

DateTime::Format::Strptime takes date/time strings and parses them into DateTime objects.


use strict;
use warnings;

use DateTime::Format::Strptime;

my $parser = DateTime::Format::Strptime->new(
  pattern => '%B %d, %Y %I:%M %p %Z',
  on_error => 'croak',

my $dt = $parser->parse_datetime('October 28, 2011 9:00 PM PDT');

print "$dt\n";

The character sequences used in the pattern are POSIX standard. See ‘man strftime’ for details.

Leave a Comment