in reply to How to get time in perl?

use Time::Localtime; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); $year = $year + 1900; $mon = $mon+1; if($hour > 12) { $hour = $hour - 12; $x = 'PM'; } elsif($hour == 12) { $x = 'PM'; } else { $x = 'AM'; } print sprintf("%02d", $mday).'/'.sprintf("%02d", $mon).'/'.sprintf("%0 +4d", $year).' '.sprintf("%02d", $hour).':'.sprintf("%02d", $min).':'. +sprintf("%02d", $sec).' '.$x;
Update: Suggest by jbrugger. Thanks jbrugger

Replies are listed 'Best First'.
Re^2: How to get time in perl?
by jbrugger (Parson) on Jul 06, 2005 at 06:35 UTC
    A little comment gube
    Month = 0-based, so to display properly use $month++;
    The pragma use Time::Localtime is not needed here, and please use -w and scrict.
    eg:
    #!/usr/bin/perl -w use strict; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(t +ime); $year = $year + 1900; $mon++; my $x = ""; if($hour > 12) { $hour = $hour - 12; $x = 'PM'; } else { $x = 'AM'; } print "$mday/$mon/$year $hour:$min:$sec $x\n";
    "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.