Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to make minutes two decimal places? i'm using localtime...

Status
Not open for further replies.

spewn

Programmer
May 7, 2001
1,034
0
0
here's the code i'm using:

Code:
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime time;
$year += 1900;
$mon +=1;
$theDateIs=$mon.'/'.$mday.'/'.$year;
$theTimeIs=$hour.':'.$min.':'.$sec;

when the minutes are less than 10, it reads as a single digit...

any ideas?

- g
 
Well, one way to force left padding of numbers is to use sprintf:

Code:
[blue]$secs[/blue] = [url=http://perldoc.perl.org/functions/sprintf.html][black][b]sprintf[/b][/black][/url] [red]"[/red][purple]%02d[/purple][red]"[/red], [blue]$secs[/blue][red];[/red]

However, you should instead simply use strftime of POSIX for formatting of datetimes:

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]POSIX[/green] [red]qw([/red][purple]strftime[/purple][red])[/red][red];[/red]

[black][b]use[/b][/black] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]@today[/blue] = [url=http://perldoc.perl.org/functions/localtime.html][black][b]localtime[/b][/black][/url][red];[/red]

[black][b]my[/b][/black] [blue]$theDateIs[/blue] = strftime [red]"[/red][purple]%m/%d/%Y[/purple][red]"[/red], [blue]@today[/blue][red];[/red]
[black][b]my[/b][/black] [blue]$theTimeIs[/blue] = strftime [red]"[/red][purple]%H:%M:%S[/purple][red]"[/red], [blue]@today[/blue][red];[/red]

[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][blue]$theDateIs[/blue] [blue]$theTimeIs[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
Core (perl 5.8.8) Modules used :
[ul]
[li]POSIX - Perl interface to IEEE Std 1003.1[/li]
[/ul]
[/tt]

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top