在 Perl 中比较日期的最佳方法是什么? [英] What is the best way to compare dates in Perl?

查看:75
本文介绍了在 Perl 中比较日期的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要读取 2 个日期并进行比较.

I need to read 2 dates and compare them.

一个日期是current_date(年、月、日)另一个是由业务逻辑决定的.然后我需要比较这两个日期,看看一个日期是否在另一个日期之前.

One date is the current_date (year,month,date) the other is determined by the business logic. I then need to compare the 2 dates and see if one is before the other.

我如何在 Perl 中做同样的事情?

How can I do the same in Perl?

我正在寻找好的文档,但我在 Perl 中发现了很多 Date 模块.不知道哪个适合它.

I am searching for good documentation, but I am finding so many Date modules in Perl. Don't know which one is appropriate for it.

推荐答案

最受欢迎的日期模块之一是 DateTime 将处理所有极端情况和其他与日期数学相关的问题.

One of the most popular date modules is DateTime which will handle all of the corner cases and other issues surrounding date math.

此链接是 DateTime 的常见问题解答,可以帮助您入门:

This link is a FAQ for DateTime which may help you get started:

DateTime 模块的工作方式是将日期(可能是字符串)转换为 DateTime 对象,然后您可以使用多个 DateTime 对象之一进行比较code>DateTime 方法.

The way the DateTime module works is that you convert your dates (which will presumably be strings) into DateTime objects, which you can then compare using one of several DateTime methods.

在下面的示例中,$dt1$dt2DateTime 对象.

In the examples below, $dt1 and $dt2 are DateTime objects.

$days 是两个日期之间的差值:

$days is the delta between the two dates:

my $days = $dt1->delta_days($dt2)->delta_days;

$cmp 是 -1、0 或 1,具体取决于 $dt1 是小于、等于还是大于 $dt2.

$cmp is -1, 0 or 1, depending on whether $dt1 is less than, equal to, or more than $dt2.

my $cmp = DateTime->compare($dt1, $dt2);

这篇关于在 Perl 中比较日期的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆