perl从日期减去一个月 [英] perl subtract one month from date
问题描述
我以格式%dd。%mm。%YYYY获取日期,我试图使用Perl减1个月。
I get the dates in format %dd.%mm.%YYYY and I'm trying to substract one month, using Perl.
示例:12.07.2013 - > 12.06.2013,30.09.2013 - > 31.08.2013
examples: 12.07.2013 -> 12.06.2013 , 30.09.2013 -> 31.08.2013
我应该使用Date :: Calc吗?任何想法?
Should I use Date::Calc? Any ideas?
谢谢
推荐答案
您可以使用 Time :: Piece
,这是Perl v5.9.5以来的核心模块。
You can use Time::Piece
, which is a core module since 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");
给定参数 12.07.2013
code> 30.09.2013 此代码打印 11.06.2013
和 30.08.2013
。
Given the arguments 12.07.2013
and 30.09.2013
this code prints 11.06.2013
and 30.08.2013
, respectively.
strptime
函数根据模板将字符串解析为 Time :: Piece
对象。然后我们可以简单地添加/减去对象来操纵日期。在这里,我使用的是一个常数,来自 Time :: Seconds
模块,对应一个月。
The strptime
function parses the string according to the template into a Time::Piece
object. Then we can simply add/subtract to the object to manipulate the date. Here I am using a constant from the Time::Seconds
module, corresponding to one month.
这些都是从 Time :: Piece
。
This is all taken from the documentation for Time::Piece
.
这篇关于perl从日期减去一个月的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!