gpt4 book ai didi

Perl DateTime 持续时间格式不显示年份

转载 作者:行者123 更新时间:2023-12-02 09:22:56 24 4
gpt4 key购买 nike

我在计算两个日期之间的差异时遇到问题。

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";

输出是这样的:

Last update date: 2015/01/13 Last update was: 0 years, 22 months, 0 days ago.

不显示 1 年 10 个月 0 天前。

最佳答案

您需要在 DateTime::Format::Duration 对象中启用 normalise 选项,如下所示

my $dfd = DateTime::Format::Duration->new(
pattern => '%Y years, %m months, %e days',
normalise => 1,
);

关于Perl DateTime 持续时间格式不显示年份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40572610/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com