?
Solved

Help with Date in perl date script.

Posted on 2006-07-17
4
Medium Priority
?
405 Views
Last Modified: 2012-06-27
Hi All,

I could do with some help...

I have a perl script which prints the time and todays date:

@months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
@weekDays = qw(Sun Mon Tue Wed Thu Fri Sat Sun);
($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime();
$year = 1900 + $yearOffset;
$theTime = "$hour:$minute, $weekDays[$dayOfWeek] $months[$month] $dayOfMonth, $year";


When it prints the time, ten minutes to two O'clock and 23 milliseconds for example it prints : 13:50:23 and this is fine

But when the time is say, five minutes past two o'clock and 8 milliseconds for example it prints:

14:5:8 and this looks awfull.

Basically whenever either the minute or milliseconds time is less than 10 (two digits) it only prints one digit what i need it to print is: 14:05:08 for example - put a zero before the digit if less than 10.

How do i change the above script to print the corretly formatted time?

P.s i have shortented my example but i do want the full day month and year including in as well.

Thanks
0
Comment
Question by:spiroosx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 39

Accepted Solution

by:
Adam314 earned 700 total points
ID: 17122234
Use sprintf instead of to format your $theTime string:


$theHour = sprintf("%02u:%02u",$hour,$minute);
0
 

Expert Comment

by:harry_bela
ID: 17122545
Thanks adam,

That works great.

0
 

Author Comment

by:spiroosx
ID: 17122563
Adam314 thanks for your help.

SpiroOSX
0
 
LVL 39

Expert Comment

by:Adam314
ID: 17123396
glad it works.  For more formatting options, see here:
http://perldoc.perl.org/functions/sprintf.html
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question