Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Representing a single-digit number as two-digits

by nine9 (Novice)
on Dec 30, 1999 at 14:45 UTC ( [id://1573]=perlquestion: print w/replies, xml ) Need Help??

nine9 has asked for the wisdom of the Perl Monks concerning the following question:

OK... I finally worked out how to get the date at about 11pm (GMT) last night...before I looked here! Grrrr...! (See Currdate.pl)...

The problem now is that if, say, the time is 11:05am, it will say 11:5. How do I tell it to make the 5 into 05. (e.g. in GW-BASIC one could use the print using ##</T>> statement...)

~nine9

  • Comment on Representing a single-digit number as two-digits

Replies are listed 'Best First'.
RE: Representing a single-digit number as two-digits
by Anonymous Monk on Dec 30, 1999 at 23:12 UTC
    sprintf("%02d",$val); # perldoc -f sprintf, if you have perl5. man perlfunc if you have perl4 or below.
RE: Representing a single-digit number as two-digits
by Anonymous Monk on Dec 30, 1999 at 22:09 UTC
    Before you head down that road, have you tried the POSIX::strftime module/function yet? The strftime time & date conversion formats will handle anything you want to do with the date, without requiring you to reinvent the wheel... "%M" for example, is minute with leading 0s...
Re: Representing a single-digit number as two-digits
by Anonymous Monk on Dec 30, 1999 at 23:26 UTC
    You could always do this kind of thing:
    if ($hour < 10) { $hour = "0$hour"; } if ($min < 10) { $min = "0$min"; } if ($sec < 10) { $sec = "0$sec"; }
    etc...
Re: Representing a single-digit number as two-digits
by Anonymous Monk on Dec 30, 1999 at 23:26 UTC
    You could always do this kind of thing:

      if ($hour < 10) { $hour = "0$hour"; }
      if ($min < 10) { $min = "0$min"; }
      if ($sec < 10) { $sec = "0$sec"; }
    


    etc...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1573]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2024-04-23 13:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found