Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Date String Conversion?

by batcater98 (Acolyte)
on Jun 08, 2007 at 13:30 UTC ( #620018=perlquestion: print w/ replies, xml ) Need Help??
batcater98 has asked for the wisdom of the Perl Monks concerning the following question:

Does anyone have a quick and clean way to convert the below "String" Date format into a MM-DD-YYYY type format?
Input: 16May07 Output: 05-16-2007
Thanks, Ad.

Comment on Date String Conversion?
Download Code
Re: Date String Conversion?
by citromatik (Curate) on Jun 08, 2007 at 13:39 UTC

    use Date::Format

    DESCRIPTION: This module provides routines to format dates into ASCII strings. They correspond to the C library routines strftime and ctime.

    To obtain the date in the format you asked:

    perl -e 'use Date::Format;print time2str("%d-%m-%Y\n", time);'

    citromatik

    UPDATE:Since the input date is fixed in format (e.g: 16May07) Date::Format can't be used. The solution that gives tirwhan seems correct

Re: Date String Conversion?
by naikonta (Curate) on Jun 08, 2007 at 13:44 UTC
    Take a look at the Q&A on date and time and see for yourself which one is quick and clean for you.

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Re: Date String Conversion?
by tirwhan (Abbot) on Jun 08, 2007 at 13:46 UTC

    Did you search CPAN? You could use Date::Manip

    use Date::Manip; use strict; use warnings; my $date=ParseDate("16May07") print UnixDate($date,"%m-%d-%Y");

    Update: fixed date format string, thanks naikonta


    All dogma is stupid.
Re: Date String Conversion?
by philcrow (Priest) on Jun 08, 2007 at 14:21 UTC
Re: Date String Conversion?
by davis (Vicar) on Jun 08, 2007 at 14:45 UTC

    ...into a MM-DD-YYYY type format?
    You may wish to reconsider that date format -- it's poorly understood outside of the US, and often ambiguous. ISO 8601 (YYYY-MM-DD) is often a much better choice.

    cheers


    davis
    Kids, you tried your hardest, and you failed miserably. The lesson is: Never try.

      Yep, this or the numeric YYYYMMDD format, which also happens to be ultimately good when sorting :)

      I wish I could have voted twice for this reply


      Enjoy, Have FUN! H.Merijn
Re: Date String Conversion?
by citromatik (Curate) on Jun 08, 2007 at 15:15 UTC

    You can easily do it yourself:

    use strict; use warnings; my $str = shift @ARGV; my %months = ( 'Jan' => '01', 'Feb' => '02', 'Mar' => '03', 'Apr' => '04', 'May' => '05', 'Jun' => '06', 'Jul' => '07', 'Aug' => '08', 'Sep' => '09', 'Oct' => '10', 'Nov' => '11', 'Dec' => '12' ); my ($year,$month,$day) = (substr ($str,-2,2,""),substr($str,-3,3,""),$ +str); my $modstr = join '-',($months{$month},length $day == 1 ? '0'.$day : $ +day, '20'.$year); print $modstr."\n"; # Run it!: # perl date_convert.pl 16May07 # 05-16-2007 # perl date_convert.pl 4Dec03 # 12-04-2003

    But be careful with years < 2000! :-)

    citromatik

      Save yourself :-)
      #!/usr/bin/perl use strict; use warnings; my %months = ( Jan => 1, Feb => 2, Mar => 3, # ..... ); my $str = shift; my($day, $mon, $year) = split /(\D+)/, $str; printf "%02d-%02d-%s\n", $months{$mon}, $day, "20$year";
      I don't feel good with the 20$year part, though. It reminds me of the 19100 syndrome :-)

      Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

        By your "Save yourself" I was thinking you were going to build the month name-number hash more lazily...

        my %months; @months{qw( jan feb mar apr may jun jul aug sep oct nov dec )} = ( 1 .. 12 );

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2014-09-17 23:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (100 votes), past polls