Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Convert Date formats

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

Please help me out in converting the date format from NOV-07 to 2007/11

I tried .....

use strict; use Date::Manip; my $thingy = 'NOV-07'; my $date = UnixDate( ParseDate($thingy), '%Y-%m-%d' );

Comment on Convert Date formats
Download Code
Re: Convert Date formats
by Utilitarian (Vicar) on Feb 03, 2010 at 10:06 UTC
    Quick and ugly test
    perl -e ' @months=qw(JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC); $date_in="NOV-07"; ($month,$year)=$date_in=~/([A-Z]{3})-(\d{2})/; for $index (0..@months){ print "20$year/",++$index,"\n" if $month eq $months[$index]; } '

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: Convert Date formats
by Ratazong (Prior) on Feb 03, 2010 at 10:10 UTC

    The following should do the trick ...

    sub dateconvert # converts dates from MMM-YY -> 20YY/MM { my %mon = ( "Jan" => 1, "Feb" => 2, "Mar" => 3, "Apr" => 4, "May" => + 5, "Jun" => 6, "Jul" => 7, "Aug" => 8, "Sep" => 9, "Oct" => 10, "Nov +" => 11, "Dec" => 12 ); $_[0] =~ /(\w\w\w)\-(\d\d)/; return sprintf("20%02d.%02d",$2,$mon{$1}); }
Re: Convert Date formats
by almut (Canon) on Feb 03, 2010 at 10:42 UTC

    You could prepend a day (e.g. 01) to help ParseDate() disambiguate the input format:

    print UnixDate( ParseDate( 'NOV-07'), '%Y-%m-%d' ), "\n"; print UnixDate( ParseDate('01-NOV-07'), '%Y-%m-%d' ), "\n"; __END__ 2010-11-07 2007-11-01

    Also, why do you specify '%Y-%m-%d' as the output format when what you say you want is '%Y/%m'?

Re: Convert Date formats
by wfsp (Abbot) on Feb 03, 2010 at 10:50 UTC
    I've made too many mistakes coverting dates with my own code. I almost always reach for Date::Parse and Date::Format which give you the very handy str2time and time2str subs.

    In this case though, NOV-07 was too ambiguous for str2time. Adding 1- to make 1-NOV-07 fixed it.

    #!/usr/bin/perl use warnings; use strict; use Date::Format; use Date::Parse; my $thingy = q{NOV-07}; my $time = str2time(qq{1-$thingy}); my $date = time2str(q{%Y/%m}, $time); print $date;
    2007/11
Re: Convert Date formats
by biohisham (Priest) on Feb 03, 2010 at 10:58 UTC
    Using a hash to look up for the months, the values for the keys are the order of the months in the calendar while the keys are each month's name ...
    #!/usr/local/bin/perl use strict; use warnings; my %monthsOrder; @monthsOrder{qw(Jan Feb Mar Apr May Jun Jul Aug Sept oct Nov Dec)}=1.. +12; #provide a list of dates to convert my @dates = ('Nov-07', 'Aug-09', 'Jul-01', 'Feb-02'); foreach my $element (@dates){ my ($month, $year)= $element=~/(\w+)-(\d+)/; print convertMonth($month, $year),"\n"; } sub convertMonth{ my ($month, $year) = @_; for my $key(keys %monthsOrder){ return "20$year/$monthsOrder{$key} " if $month eq $key; } }
    Output:
    2007/11 2009/8 2001/7 2002/2
    Updated to account for passing an array of different dates..



    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.
Re: Convert Date formats
by 7stud (Deacon) on Feb 03, 2010 at 11:09 UTC

    Here is a way to do it with Date::Manip, although as far as I can tell your string does not contain a valid 'date'--because it does not specify a day. So you have to alter your string first:

    use strict; use warnings; use 5.010; use Date::Manip::Date; my $string = 'Nov-07'; $string .= ' 01'; #add day field to string my $format = '%b-%y %d'; my $date = new Date::Manip::Date; my $error = $date->parse_format($format, $string); if ($error == 0) { #everything worked correctly say $date->printf('%Y/%m'); } else { say "something went wrong"; if ($error == 1) { say "format didn't match your string"; } else { say "format is invalid--won't create a valid date: $error"; } } --output:-- 2007/11

    See here for details:

    http://search.cpan.org/~sbeck/Date-Manip/lib/Date/Manip/Date.pod
Re: Convert Date formats
by Krambambuli (Deacon) on Feb 03, 2010 at 11:10 UTC
    If you want to stay with Date::Manip, you'll have to feed the parser with a valid date: 'NOV-07' is not a date, but a month (there is no day specified).

    Taking care of that, the following code works:
    #!/usr/bin/perl use strict; use warnings; use Date::Manip; my $thingy = 'NOV-2007'; my $date = UnixDate( ParseDate( "1-$thingy" ), '%Y/%m' ); print "$date\n";
    Good luck,

    Krambambuli
    ---

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (11)
As of 2014-12-19 13:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (82 votes), past polls