spiroosx
asked on
Help with Date in perl date script.
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Adam314 thanks for your help.
SpiroOSX
SpiroOSX
glad it works. For more formatting options, see here:
http://perldoc.perl.org/functions/sprintf.html
http://perldoc.perl.org/functions/sprintf.html
That works great.