[thelist] Perl Date/Time

Katherine Spice k.spice at acu.ac.uk
Wed Feb 7 06:47:31 CST 2001


Hi Chris,

You can use the printf function, to specify the no. of digits the year,
month and day should have.

($sec, $min, $hrs, $day, $month, $year) = (localtime) [0,1,2,3,4,5];
printf("%04d-%02d-%02d %d:%d:%d\n", $year+1900, $month+1, $day, $hrs,
$min, $sec);

If you have it (and if not, you should get it :-) this is described in
O'Reilly's Perl Cookbook, page 73.

HTH,
Katherine


CDitty wrote:
> 
> Can anyone tell me how to get the current date/time in perl in this format
> (2001-02-07 00:45:41)?  I have been able
> to find enough info to get everything in a format close to that (2001-2-7
> 1:31:41), but I need everything
> but the year in 2 digits.
> 
> Here is my code.   It's very rough.  If anyone can shorten this, be my
> guest.  :)
> 
> print "Content-type: text/html\n\n";
> $mytime1 = (localtime)[5]; # year
> $mytime1 = $mytime1 + 1900;
> $mytime2 = (localtime)[4]; # month
> $mytime2 = $mytime2 + 1;
> $mytime3 = (localtime)[3]; # day
> $mytime4 = (localtime)[2]; # hour
> $mytime5 = (localtime)[1]; # minute
> $mytime6 = (localtime)[0]; # second
> 
> print "$mytime1-$mytime2-$mytime3 $mytime4:$mytime5:$mytime6 \n";
> 
> Any help is appreciated.
> 
> Thanks
> 
> Chris
ers of the Web, evolt !




More information about the thelist mailing list