in reply to Re^2: Date::Parse - how to correctly parse dates between 1901 and 1969
in thread Date::Parse - how to correctly parse dates between 1901 and 1969

The input can be in variable formats:

my @dates = ( "2018-02-20 00:00:00", "20180220", "02/20/2018", "02/20/18", # interpreted as 1918-02-20 "2018-02-20" );

Date::Parse handles those (with some massaging). DateTime::Format::Strptime requires well formatted dates to begin with.

Am I missing a module of DateTime or Time::Piece that can handle the different input formats?

  • Comment on Re^3: Date::Parse - how to correctly parse dates between 1901 and 1969
  • Download Code

Replies are listed 'Best First'.
Re^4: Date::Parse - how to correctly parse dates between 1901 and 1969
by Anonymous Monk on Feb 20, 2018 at 21:10 UTC
    #!/usr/bin/perl -- use strict; use warnings; use DateTime; use DateTime::Format::Natural; my @dates = ( "2018-02-20 00:00:00", "20180220", "02/20/2018", "02/20/18", # interpreted as 1918-02-20 "2018-02-20", "today", "now", ); my $dfn = DateTime::Format::Natural->new; print $dfn->parse_datetime($_),$/ for @dates; __END__ 2018-02-20T00:00:00 2018-02-20T21:12:19 2018-02-20T21:12:19 2018-02-20T21:12:19 2018-02-20T00:00:00 2018-02-20T00:00:00 2018-02-20T21:12:19
Re^4: Date::Parse - how to correctly parse dates between 1901 and 1969
by Anonymous Monk on Feb 20, 2018 at 19:04 UTC
    So you simply make up a pattern for each format ... ... Could try ..Format::Natural