Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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

by eniad (Acolyte)
on Feb 20, 2018 at 15:30 UTC ( [id://1209590]=note: print w/replies, xml ) Need Help??


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

Can Date::Manip parse variable input formats? I have gotten inputs like:

my @dates = ( "2018-02-20 00:00:00", "20180220", "02/20/2018", "02/20/18", # interpreted as 1918-02-20 "2018-02-20" );
  • Comment on Re^2: Date::Parse - how to correctly parse dates between 1901 and 1969
  • Download Code

Replies are listed 'Best First'.
Re^3: Date::Parse - how to correctly parse dates between 1901 and 1969
by thanos1983 (Parson) on Feb 20, 2018 at 15:56 UTC

    Hello again eniad,

    Well a simple example would answer your question:

    #!/usr/bin/perl use strict; use warnings; use Date::Manip; use feature 'say'; my @dates = ( "2018-02-20 00:00:00", "20180220", "02/20/2018", "02/20/18", # interpreted as 1918-02-20 "2018-02-20", "today"); say UnixDate( ParseDate($_), "%Y-%m-%d") for (@dates); __END__ $ perl test.pl 2018-02-20 2018-02-20 2018-02-20 2018-02-20 2018-02-20 2018-02-20

    So in conclusion, yes the module can parse all the dates that you provided.

    Update: If you want to know which date formats are acceptable from the module read here Date::Manip::Date/VALID DATE FORMATS. On the same link you will find time formats but also date and time formats combined.

    Update 2: A minor similar example on how to parse time and print also time zone if you are interested:

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!

      It even correctly handles the 2-digit year example. Nice!

      I'll take a deeper dive into Date::Manip. Thanks for the example.

      Date::Manip greatly simplified the parsing of the datetimes. See Solution 2 in my answer. Thanks for the help!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1209590]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-19 06:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found