Format Date/Time in Perl

Discussion in 'Perl' started by pradeep, Dec 17, 2008.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    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:
    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:
    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:
    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.
     
  2. debleena_doll2002

    debleena_doll2002 New Member

    Joined:
    Feb 5, 2008
    Messages:
    119
    Likes Received:
    0
    Trophy Points:
    0
    Nice article....I have taken printout of it ...n will read @ home..
     
  3. NDL

    NDL New Member

    Joined:
    Oct 20, 2008
    Messages:
    71
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    SL,colombo
    Home Page:
    http://www.nisal.co.nr
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  5. ruby20

    ruby20 New Member

    Joined:
    Dec 4, 2008
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Nice article.....
     
  6. unni krishnan.r

    unni krishnan.r Member

    Joined:
    Apr 20, 2010
    Messages:
    209
    Likes Received:
    4
    Trophy Points:
    18
    Occupation:
    education
    Location:
    Kerala
    Home Page:
    http://blogofunni.blogspot.com
  7. AllyUnion

    AllyUnion New Member

    Joined:
    Jun 20, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    There is a bug in the code...

    "$abbr[$mon] $mday";
    Should be:
    "$month_abbr[$mon] $mday";
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice