我在两个日期之间的差异计算方面遇到了麻烦。
my $todayDate = DateTime->now;
my @updateDateFields = split /\//, $proteinObj->{lastUpdate}; #yyyy/mm/dd
my $updateDateTime = DateTime->new(
year => @updateDateFields[0],
month=> @updateDateFields[1],
day=> @updateDateFields[2]
);
my $daysSinceLastUpdate = $todayDate - $updateDateTime;
my $dfd = DateTime::Format::Duration->new(pattern => '%Y years, %m months, %e days');
print "Last update was: ". $dfd->format_duration($daysSinceLastUpdate). " ago.\n";
输出是这样的:
上次更新日期:2015/01/13最近更新时间:0年,22个月,0 几天前。
它不显示1年,10个月,0天前。
答案 0 :(得分:5)
您需要在normalise
对象中启用DateTime::Format::Duration
选项,例如
my $dfd = DateTime::Format::Duration->new(
pattern => '%Y years, %m months, %e days',
normalise => 1,
);