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

Extract date and time

by jai_dgl (Beadle)
on Jun 12, 2008 at 05:52 UTC ( [id://691591]=perlquestion: print w/replies, xml ) Need Help??

jai_dgl has asked for the wisdom of the Perl Monks concerning the following question:

How do I extract date and time from a text content and convert it into a standard format. Example 1. Today's date is June 12 , 2008 I need to extract the date Example 2. Today's date is 12/06/08, how do I extract date from this ------------------------------------------------------------ I need to extract date from the both as well as any other format also.

Replies are listed 'Best First'.
Re: Extract date and time
by Erez (Priest) on Jun 12, 2008 at 06:39 UTC
Re: Extract date and time
by pc88mxer (Vicar) on Jun 12, 2008 at 06:53 UTC
    Here's a solution using Date::Parse:
    use Date::Parse; while (<>) { chomp; my @t = strptime($_); my $d = sprintf("%d/%d/%d", $t[3], $t[4]+1, $t[5]+1900); print "$_ -> $d\n"; } __END__ June 12, 2008 -> 12/6/2008

    Some caveats: Date::Parse only recognizes English month names and follows American conventions for things like 12/6/08 which parses as December 6th, 2008. Otherwise it will recognize most variations of specifying dates, e.g. (12 Jun 2008, 2008-06-12, etc.)

Log In?
Username:
Password:

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

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

    No recent polls found