How do I get the current time in hh:nn:ss format in Perl?

I’ve looked over the Perldocs at localtime but I’m still pretty confused about how to get the current time in the hh:nn:ss format, such as 20:17:54, or 09:45:00.

Could someone point me in the right direction?

 StumbleUpon It!

Technorati Tags: , , , , ,

2 Responses to “How do I get the current time in hh:nn:ss format in Perl?”

  1. om says:

    Get the current local time broken out into individual variables:

      ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime();

    then construct a string that contains the hour, minute and second in colon-separated two-digit fields:

      $hhmmss = sprintf( "%02d:%02d:%02d", $hour, $min, $sec );

    To confirm that it worked, print that string:

      print $hhmmss;

  2. nwboy74 says:

    Also, you don’t need to save anything after hour from the returned array with the localtime call. So you can shorten the call to

    my ($sec, $min, $hour) = localtime();

Leave a comment or a question

You must be logged in to post a comment.

Powered by Yahoo! Answers