Learn how to Make Money Online | Free Tech Magazines
Go4Expert
Go4Expert RSS Feed

Go Back   Programming and SEO Forum >  Go4Expert > Articles / Source Code > Programming > Perl

Discuss / Comment Copy HTML to Clipboard  Copy BBCode to Clipboard  Add to del.icio.us  Add to Google  Digg it  Add to Yahoo !  Add to Windows Live  Add to Facebook  Add to StumbleUpon 
 
Bookmarks Article Tools Search this Article Display Modes

Format Date/Time in Perl

By pradeep pradeep is offline

On 17th December, 2008
Lightbulb Format Date/Time in Perl

ADVERTISEMENT
Show Printable Version Email this Page Subscription Add to Favorites Copy Format Date/Time in Perl link

Author

pradeep ( Team Leader )

Yet to provide details about himself


All articles By pradeep

Recent Articles

Similar Articles

Working with dates is a very common feature of any application, be it a web app or a desktop one. Some people consider Perl to be a language where working with dates is hard, but basically it's quite easy if you know how to. Datetime is a common term while refering to date and time together.

Basics

Basic formatting of datetime in Perl is done by writing your own formatting logic, we'll use the localtime function to get the current datetime details. For more info about the locatime function checkout http://perldoc.perl.org/functions/localtime.html

Code: Perl
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year += 1900; ## $year contains no. of years since 1900, to add 1900 to make Y2K compliant
my @month_abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );

print "$abbr[$mon] $mday"; ## gives Dec 17

Enter POSIX::strftime

The strftime function helps to format time information to string using pre-defined formatting parameters. Here's how we go about it, you might need a list of formatting string codes found at http://www.mkssoftware.com/docs/man3/strftime.3.asp

Code: Perl
use POSIX qw/strftime/;

print strftime('%D %T',localtime); ## outputs 12/17/08 10:08:35
print strftime('%d-%b-%Y %H:%M',localtime); ## outputs 17-Dec-2008 10:08

You can format the way you like it using the format parameters, I personally feel this is the easiest way to format datetime in Perl for daily use.

Using Date::Calc

The Perl module Date::Calc's primary use is date manipulation, but it may also be used formatting dates to some extent, you may read the module's docs at http://search.cpan.org/~stbey/Date-Calc-5.4/Calc.pod

Code: Perl
use Date::Calc qw/Date_to_Text Date_to_Text_Long Today/;

my @date_today = Today();

print Date_to_Text(@date_today);
print Date_to_Text_Long(@date_today);

Hope this write-up was helpful. Happy coding.
Old 12-19-2008, 10:35 AM   #2
debleena_doll2002
Ambitious contributor
 
Join Date: Feb 2008
Posts: 119
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 2
debleena_doll2002 is on a distinguished road

Re: Format Date/Time in Perl


Nice article....I have taken printout of it ...n will read @ home..
debleena_doll2002 is offline   Reply With Quote
Old 12-21-2008, 12:53 PM   #3
NDL
Contributor
 
NDL's Avatar
 
Join Date: Oct 2008
Location: SL,colombo
Posts: 71
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 2
NDL is on a distinguished road
Send a message via Yahoo to NDL

Re: Format Date/Time in Perl


Nice
NDL is offline   Reply With Quote
Old 01-06-2009, 12:24 PM   #4
shabbir
Go4Expert Founder
 
shabbir's Avatar
 
Join Date: Jul 2004
Location: On Earth
Posts: 10,943
Thanks: 35
Thanked 167 Times in 139 Posts
Rep Power: 10
shabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud of
Send a message via Yahoo to shabbir

Re: Format Date/Time in Perl


shabbir is offline   Reply With Quote
Old 01-10-2009, 03:40 PM   #5
ruby20
Light Poster
 
Join Date: Dec 2008
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
ruby20 is on a distinguished road

Re: Format Date/Time in Perl


Nice article.....
__________________
Download NY Web Development
ruby20 is offline   Reply With Quote
Discuss / Comment Copy HTML to Clipboard  Copy BBCode to Clipboard  Add to del.icio.us  Add to Google  Digg it  Add to Yahoo !  Add to Windows Live  Add to Facebook  Add to StumbleUpon 


Currently Active Users Reading This Article: 2 (0 members and 2 guests)
 
Article Tools Search this Article
Search this Article:

Advanced Search
Display Modes
Bookmarks

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads / Articles
Thread Thread Starter Forum Replies Last Post
Why is it throwing "number format exception" ? Kailash Java 3 02-14-2008 02:42 PM
PERL - The String Form: Expression Evaluation shree Perl 4 08-25-2007 07:18 PM
Shortcuts in Perl pradeep Perl 1 04-23-2007 10:05 PM
PERL resources coderzone Perl 3 08-02-2006 12:53 AM
XML parsing in Perl pradeep Perl 0 05-19-2006 07:19 PM

 

All times are GMT +5.5. The time now is 04:24 AM.