For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > January 2006 > Date formatting









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Date formatting
stagepin

2006-01-10, 4:02 am

I am a Perl newbie and I would like to ask for some advice.

I need to manipulate the date format.
I receive $date in this format: yyyymmdd (For example: 20051230)
I need to display it as "December 30, 2005"

This is what I came up with to accomplish this task:

-----------------------------------------------------------------------------------------------
my @items = $date =~ m/(\d{4})(\d{2})(\d{2})/;
my $formatted_date = POSIX::strftime("%B %e, %Y",
localtime(timelocal(0,0,0,$items[2],$ite
ms[1]-1,$items[0])));
-----------------------------------------------------------------------------------------------

The above code is working just fine, but I am wondering if this is
actually a good way of doing this? Is there a more elegant way?

Any advice or comments would be appreciated!

Thank you
stagepin

usenet@DavidFilmer.com

2006-01-10, 4:02 am

stagepin wrote:
> I receive $date in this format: yyyymmdd (For example: 20051230)
> I need to display it as "December 30, 2005"


How about using the Date::Manip module, which does nearly everything
you could ever want to do regarding time/dates:

#!/usr/bin/perl
use Date::Manip;
print UnixDate ('20051230', "%B %e, %Y");
__END__

If you want abbreviated months ('Dec' instead of 'December') use "%b"
instead of "%B". You didn't state a display preference about
single-digit days (ie, 'Dec 5' vs 'Dec 05'); this will show
single-digit days without a leading zero (use "%d" instead of "%e" if
you want leading zeros on single-digit days). See
http://search.cpan.org/~sbeck/DateManip-5.44/Manip.pod

Cheers!

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com