perl从日期减去一个月

ire*_*rek 3 perl date

我以格式%dd.%mm.%YYYY获取日期,我试图使用Perl减去一个月.

示例:12.07.2013 - > 2013年6月12日,2013年9月30日 - > 2013年8月31日

我应该使用Date :: Calc吗?有任何想法吗?

谢谢

TLP*_*TLP 6

您可以使用Time::Piece,这是自Perl v5.9.5以来的核心模块.

use strict;
use warnings;
use Time::Piece;
use Time::Seconds;

my $t = Time::Piece->strptime(shift, "%d.%m.%Y");
$t -= ONE_MONTH;
print $t->strftime("%d.%m.%Y");
Run Code Online (Sandbox Code Playgroud)

鉴于参数12.07.201330.09.2013此代码分别打印11.06.201330.08.2013.

strptime函数根据模板将字符串解析为Time::Piece对象.然后我们可以简单地添加/减去对象来操纵日期.这里我使用Time::Seconds模块中的常量,对应一个月.

这些都来自文档Time::Piece.