方法一:
use POSIX qw(strftime);
my $timeStr1 = strftime "%Y-%m-%d", localtime;
my $timeStr2 = strftime "%Y-%m-%d", localtime(time-86400); (前一天)
print "$timeStr \n";
方法二:
#!/usr/bin/perl
($sec,$min,$hour,$day,$mon,$year,$weekday,$yeardate,$savinglightday)
= (localtime(time));
$sec = ($sec < 10)? "0$sec":$sec;
$min = ($min < 10)? "0$min":$min;
$hour = ($hour < 10)? "0$hour":$hour;
$day = ($day < 10)? "0$day":$day;
$mon = ($mon < 9)? "0".($mon+1):($mon+1);
$year += 1900;
$today = "$day.$mon.$year|$hour:$min:$sec";
print $today."\n";
print time."\n";
如果要输出一天前的日期,将time减去一天的秒数(86400)。
(localtime(time-86400));
#!/usr/bin/perl
print &get_time(10)."\n";
sub get_time {
$interval = $_[0]*60;
($sec,$min,$hour,$day,$mon,$year,$weekday,$yeardate,$savinglightday)
= (localtime(time + $interval));
$sec = ($sec < 10)? "0$sec":$sec;
$min = ($min < 10)? "0$min":$min;
$hour = ($hour < 10)? "0$hour":$hour;
$day = ($day < 10)? "0$day":$day;
$mon = ($mon < 9)? "0".($mon+1):($mon+1);
$year += 1900;
return "$year-$mon-$day $hour:$min:$sec.00";
}
sub getTime(){
(my $sec,my $min,my $hour,my $day,my $mon,my $year,my $weekday,my $yeardate,my $savinglightday)
= (localtime(time));
$sec = ($sec < 10)? "0$sec":$sec;
$min = ($min < 10)? "0$min":$min;
$hour = ($hour < 10)? "0$hour":$hour;
$day = ($day < 10)? "0$day":$day;
$mon = ($mon < 9)? "0".($mon+1):($mon+1);
$year += 1900;
my $now = "$year$mon$day $hour:$min:$sec ";
return $now;
}
小白