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

Hello Monks,

I'm working with sybase, and a date is being returned to me as:
Dec 11 2003 12:00AM
I would like to change this to a format of mm/dd/yy
(12/11/03)
Is there a module that can do this easily for me?

Thanks a lot,
Jonatham

Replies are listed 'Best First'.
Re: date reformatting
by bpphillips (Friar) on Sep 16, 2004 at 15:23 UTC
    you need two things, first the parsing of a string into a unix time and then the formating of that time into a string. Some modules undoubtedly will do both but I'm familiar with Date::Parse and Date::Format.

    Here's a quick example:
    use Date::Parse; use Date::Format; my $date = "Dec 11 2003 12:00AM"; my $time = str2time($date); my $new_date = time2str("%D",$time);
Re: date reformatting
by davorg (Chancellor) on Sep 16, 2004 at 16:29 UTC

    A lot of very clever people have been working hard to replace Perl's confusing mish-mash of date and time modules with a consistant set of modules. The results of their labours are at http://datetime.perl.org/ and you can get all of their modules from CPAN.

    Using their modules, you can solve your problem like this:

    #!/usr/bin/perl use strict; use warnings; use DateTime::Format::Strptime; my $strp = DateTime::Format::Strptime->new(pattern => '%b %d %Y %I:%M% +p'); my $string = 'Dec 11 2003 12:00AM'; my $dt = $strp->parse_datetime($string) or die; $string = $dt->strftime('%m/%d/%Y'); # warning! illogical format!! print "$string\n";
    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: date reformatting
by herveus (Prior) on Sep 16, 2004 at 16:21 UTC
    Howdy!

    You could use the convert function in your SQL to return it as a string in a whole variety of formats, including mm/yy/dd.

    Yeah, it's not a direct answer to your question, but it might be simpler.

    yours,
    Michael
Re: date reformatting
by bobf (Monsignor) on Sep 16, 2004 at 16:09 UTC

    In addition to the Date:: modules mentioned above, you could also look into Date::Manip. It's a pretty big module, but it can do a lot!

    use strict; use warnings; use Date::Manip; print UnixDate( ParseDate( 'Dec 11 2003 12:00AM' ), "%m/%d/%y" );

    HTH

    Update: Added code example

Re: date reformatting
by thor (Priest) on Sep 16, 2004 at 17:53 UTC
    Here's a Sybase-specific solution to show the current date in the mm/dd/yy format:
    select convert(varchar(8), getdate(), 1)
    For your case, just replace the getdate() with whatever value you're trying to display in that format Most likely it's a column, but as I've shown here, it can be anything that returns a datetime.

    thor

    Feel the white light, the light within
    Be your own disciple, fan the sparks of will
    For all of us waiting, your kingdom will come

Re: date reformatting
by Grygonos (Chaplain) on Sep 16, 2004 at 16:37 UTC
    100% tested and verified.
    use strict; use warnings; my $date = 'Jan 19 2007 12:00AM'; 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'); $date = $1 if($date =~ m{^(\w{3}\s{1}\d{1,2}\s{1}\d{4})}); foreach my $key (%months) { if($date =~ m{^$key}) { $date =~ s{^$key}{$months{$key}}; last; } } $date =~ s{(\d{2})\s(\d{2})\s{1}\d{2}(\d{2})}{$1\/$2\/$3}; print $date;