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

前端 未结 4 1583
旧时难觅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:18

    Have a look at the Date::Parse module, i.e. the str2time() function. It has support for most of the commonly used formats.

    Example:

    use Date::Parse;
    use DateTime;
    
    my $str = "Tue, 20 Sep 2011 08:51:08 -0500";
    my $epoch = str2time($str);
    my $datetime = DateTime->from_epoch(epoch => $epoch);
    

提交回复
热议问题