我如何调整这个以使用localtime获取昨天的日期?
use strict;
sub spGetCurrentDateTime;
print spGetCurrentDateTime;
sub spGetCurrentDateTime {
my ($sec, $min, $hour, $mday, $mon, $year) = localtime();
my @abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
my $currentDateTime = sprintf "%s %02d %4d", $abbr[$mon], $mday, $year+1900; #Returns => 'Aug 17 2010'
return $currentDateTime;
}
Run Code Online (Sandbox Code Playgroud)
〜
我已经看到了其他答案,但由于某种原因,它们并不适合我.
我试图使用以下代码在Perl脚本中获取昨天的日期.
供将来参考,今天的日期是2015年11月12日
my (undef, undef, undef, $mday,$mon,$year) = localtime();
my $prev_day = timelocal(0, 0, 0, $mday-1, $mon, $year);
(undef, undef, undef, $mday,$mon,$year) = localtime($prev_day);
$year += 1900;
print "Yesterday's Day: $mday, Month: $mon, Year: $year\n";
Run Code Online (Sandbox Code Playgroud)
除了我的输出看起来像这样
Yesterday's Day: 11, Month: 10, Year: 2015.
Run Code Online (Sandbox Code Playgroud)
我应该读昨天的日期Day: 11, Month: 11, Year: 2015.为什么减去月份?
编辑:这与建议的答案不同,因为我想知道为什么当地时间似乎写错了月份.