Learn how to Make Money Online doing freelancing, Affiliate Marketing, Blogging and many more ...
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  | More
 
Bookmarks Article Tools Search this Article Display Modes

Format Date/Time in Perl


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

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.
The Following User Says Thank You to pradeep For This Useful Post:
tarunt (02-05-2010)
Old 12-19-2008, 09:35 AM   #2
Ambitious contributor
 
Join Date: Feb 2008
Posts: 119
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 3
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, 11:53 AM   #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, 11:24 AM   #4
Go4Expert Founder
 
shabbir's Avatar
 
Join Date: Jul 2004
Location: On Earth
Posts: 12,516
Thanks: 53
Thanked 276 Times in 215 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, 02:40 PM   #5
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
Old 05-31-2010, 05:06 PM   #6
Ambitious contributor
 
unni krishnan.r's Avatar
 
Join Date: Apr 2010
Location: Kerala
Posts: 169
Thanks: 13
Thanked 1 Time in 1 Post
Rep Power: 1
unni krishnan.r is on a distinguished road
Send a message via Skype™ to unni krishnan.r

Re: Format Date/Time in Perl


Nice
__________________
Orkut community of Go4expert
unni krishnan.r is offline   Reply With Quote
Discuss / Comment  Copy HTML to Clipboard  Copy BBCode to Clipboard  | More


Currently Active Users Reading This Article: 1 (0 members and 1 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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

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

 

All times are GMT +5.5. The time now is 05:28 AM.