Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Problem parsing date with Date::Manip

by kalyanrajsista (Scribe)
on Jul 27, 2010 at 09:36 UTC ( #851492=perlquestion: print w/ replies, xml ) Need Help??
kalyanrajsista has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks

I'm trying to parse date but is giving irrelevant output for me...please find the code

OUTPUT

20/Apr/30

Expected OUTPUT

30/Apr/10
#!/usr/bin/perl use strict; use Date::Manip; Date_Init("TZ=EST5EDT"); print "Hello, World...\n"; my $date = '30-04-2010'; $date = UnixDate($date, "%e/%b/%y"); print $date;

Comment on Problem parsing date with Date::Manip
Select or Download Code
Re: Problem parsing date with Date::Manip
by moritz (Cardinal) on Jul 27, 2010 at 09:57 UTC
    Maybe you would have better luck if you provided a format string that has any relation to the input format (/ vs. -).
    Perl 6 - links to (nearly) everything that is Perl 6.

      If i put date as '30/04/2010' its not printing anything

      I think UnixDate is for formatting only, like strftime
Re: Problem parsing date with Date::Manip
by Anonymous Monk on Jul 27, 2010 at 10:19 UTC
    For information on valid date formats, refer to the section VALID DATE FORMATS. For information on valid time zone information, refer to the section VALID TIME ZONE FORMATS.
    #!/usr/bin/perl -- use strict; use Date::Manip; Date_Init("TZ=EST5EDT"); print "Hello, World...\n"; my $date = '30-04-2010'; $date = UnixDate($date, "%e/%b/%y"); print "$date\n"; print scalar UnixDate($date, "%g"),"\n"; __END__ Hello, World... 20/Apr/30 Sun, 20 Apr 1930 00:00:00 EST
    I see its parsing the date wrong, but the documentation does not explain how to use the function

    Not good

Re: Problem parsing date with Date::Manip
by JavaFan (Canon) on Jul 27, 2010 at 13:28 UTC
    You should parse the date first. The first argument of UnixDate must be a date in the format Date::Manip uses internally - which isn't a string in the format you're using.
    $ perl -MDate::Manip -wE 'say UnixDate ParseDate("2010-04-30"), "%e/%m +/%y"' 30/04/10 $
Re: Problem parsing date with Date::Manip
by duelafn (Priest) on Jul 27, 2010 at 14:30 UTC

    Anonymous Monk has it right, Date::Manip does not recognize the date format (see VALID DATE FORMATS). You will need to parse/correct it yourself:

    #!/usr/bin/perl use strict; use Date::Manip; Date_Init("TZ=EST5EDT"); print "Hello, World...\n"; my $date = '30-04-2010'; die "Can't parse date" unless $date =~ s/(\d\d)\-(\d\d)\-(\d\d\d\d)/$3 +-$2-$1/; $date = UnixDate($date, "%e/%b/%y"); print $date;

    Good Day,
        Dean

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2015-07-05 15:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (67 votes), past polls