在 Perl 中进行日期算术的推荐方法是什么?

发布于 2024-09-28 01:10:59 字数 181 浏览 3 评论 0 原文

在 Perl 中进行日期算术的推荐方法是什么?

举例来说,我想知道从今天起三天前的日期(其中 today = 2010-10-17today - 3 days =2010-10-13)。你会如何在 Perl 中做到这一点?

What is the recommended way of doing date arithmetics in Perl?

Say for example that I want to know the date three days ago from today (where today = 2010-10-17 and today - 3 days = 2010-10-13). How would you do that in Perl?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

﹏雨一样淡蓝的深情 2024-10-05 01:10:59

您可以使用 DateTime 和 DateTime::Duration

http://search.cpan。 org/dist/DateTime/lib/DateTime/Duration.pm

或者使用 unix 时间戳:

my $now = time();
my $threeDaysAgo = $now - 3 * 86400;
my ($day, $mon, $year) = (localtime($threeDaysAgo))[3, 4, 5];
printf("Three days ago was %04d-%02d-%02d", $year+1900, $mon+1, $day);

You can use DateTime and DateTime::Duration

http://search.cpan.org/dist/DateTime/lib/DateTime/Duration.pm

Or work with unix timestamps:

my $now = time();
my $threeDaysAgo = $now - 3 * 86400;
my ($day, $mon, $year) = (localtime($threeDaysAgo))[3, 4, 5];
printf("Three days ago was %04d-%02d-%02d", $year+1900, $mon+1, $day);
终陌 2024-10-05 01:10:59

有很多很多不同的日期和时间操作模块。

其中包括:

所有这些都经过深思熟虑。除此之外还有很多其他的。很大程度上取决于您想要进行的算术类型。 DateTime 可能是最严格的,但 Date::Calc 和 Date::Manip 可能更容易处理您需要的工作。

There are many, many different date and time manipulation modules.

These include:

All of these are well thought of. There are many others in addition. A lot depends on the sort of arithmetic you want to do. DateTime is perhaps the most rigorous, but Date::Calc and Date::Manip may be easier to handle for the work you need.

书信已泛黄 2024-10-05 01:10:59

这是迄今为止我遇到的功能最多的模块: 日期::Manip

This is by far the module with the most functionality that I've come across: Date::Manip

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文