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...