Solved

Help with Date in perl date script.

Posted on 2006-07-17
4
397 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
  • 2
4 Comments
 
LVL 39

Accepted Solution

by:
Adam314 earned 175 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now