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

Time Piece Error Parsing

by Anonymous Monk
on Mar 09, 2016 at 16:36 UTC ( #1157182=perlquestion: print w/replies, xml ) Need Help??

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

Hi there Monks!

I am getting an error I cant understand, here:
#!/usr/bin/perl use warnings; use strict; use Time::Piece; my $t = Time::Piece->new(); my $date = $t->strptime(sprintf("%02d",$t->mon).$t->day_of_month.$t->y +ear, '%m%d%Y'); print"\n $date\n";
Error: Error parsing time at /usr/lib/perl/5.18/Time/Piece.pm

Any suggestions, I know it has to do with the "$t->year" part, just dont know how to get it to work.

Thanks for helping!

Replies are listed 'Best First'.
Re: Time Piece Error Parsing
by runrig (Abbot) on Mar 09, 2016 at 16:42 UTC
    Today is the 9th. If you're going to mash digits together without delimiters, the month AND the day of month each need to be 2 digits.
      Interesting, so this should work even if the month has two digits.:
      $date = $t->strptime(sprintf("%02d",$t->mon).sprintf("%02d",$t->day_of +_month).$t->year, '%m%d%Y');
        Alternatively:
        $date = $t->strptime(sprintf("%02d%02d%04d",$t->mon,$t->day_of_month,$ +t->year), '%m%d%Y');
Re: Time Piece Error Parsing
by poj (Abbot) on Mar 09, 2016 at 17:03 UTC

    Why strptime ? Try using strftime

    my $date = $t->strftime('%m%d%Y');

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2019-05-23 05:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you enjoy 3D movies?



    Results (142 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!