kno*_*orv 8 perl datetime cpan datediff date-arithmetic
在Perl中推荐日期算术的推荐方法是什么?
比如我想知道三天前从今天开始的日期(其中today= 2010-10-17和today - 3 days= 2010-10-13).你会如何在Perl中做到这一点?
eum*_*iro 11
您可以使用DateTime和DateTime :: Duration
http://search.cpan.org/dist/DateTime/lib/DateTime/Duration.pm
或者使用unix时间戳:
my $now = time();
my $threeDaysAgo = $now - 3 * 86400;
my ($day, $mon, $year) = (localtime($threeDaysAgo))[3, 4, 5];
printf("Three days ago was %04d-%02d-%02d", $year+1900, $mon+1, $day);
Run Code Online (Sandbox Code Playgroud)
有许多不同的日期和时间操作模块.
这些包括:
所有这些都是深思熟虑的.此外还有许多其他的.很大程度上取决于你想要做的算术类型.DateTime可能是最严格的,但Date :: Calc和Date :: Manip可能更容易处理您需要的工作.