Solved

Help with Date in perl date script.

Posted on 2006-07-17
4
399 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 …
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

912 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

18 Experts available now in Live!

Get 1:1 Help Now