使用DateTime计算日期之间的天数

时间:2019-03-26 16:25:54

标签: perl

我使用DateTime对象有2个日期,并且尝试仅使用delta_days来获得天数差异,但它无法正常工作。我以为这会给我总的天数,但是看起来好像不包括几个月的天数。

use DateTime;
use DateTime::Format::Strptime;
use DateTime::Format::Duration;

my $parseseq = DateTime::Format::Strptime->new(pattern => '%Y%m%d');
my $todaydate = "20190326";
my $lastdate  = "20190223";

my $currmark = $parseseq->parse_datetime($todaydate);
$currmark->set_formatter($parseseq);
my $lastmark = $parseseq->parse_datetime($lastdate);
$lastmark->set_formatter($parseseq);
my $diff = $currmark->subtract_datetime($lastmark);
my $diffdays = $diff->delta_days();
print "\ntodaydate $todaydate\n";
print "lastdate  $lastdate\n";
print "Diff Days $diffdays\n\n";

输出:

todaydate 20190326
lastdate  20190223
Diff Days 3

0 个答案:

没有答案