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

前端 未结 4 1606
旧时难觅i
旧时难觅i 2020-12-03 02:23

I know about the DateTime Perl module, and many of the DateTime::Format:: modules to parse specific kinds of date/time formats. However given some examples of d

4条回答
  •  时光说笑
    2020-12-03 03:21

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

    #!/usr/bin/perl
    
    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.

提交回复
热议问题