如何计算 Perl 中两个日期之间的天数?
我想计算(仅使用默认的 Perl 安装)两个日期之间的天数。 两个日期的格式都类似于 04-MAY-09。 (DD-MMM-YY)
我找不到任何讨论该日期格式的教程。 我应该为此格式构建自定义日期检查器吗? 进一步阅读 CPAN 上的 Date::Calc 看起来不太可能支持这种格式。
I want to calculate (using the default Perl installation only) the number of days between two dates. The format of both the dates are like so 04-MAY-09. (DD-MMM-YY)
I couldn't find any tutorials that discussed that date format. Should I be building a custom date checker for this format? Further reading of the Date::Calc on CPAN it looks unlikely that this format is supported.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
似乎有相当多的混乱,因为根据您想要实现的目标,“两个日期之间的天数”可能意味着至少两个不同的事物:
作为示例并注意差异,假设您有两个构造如下的 DateTime 对象:
请注意,
$dt1
在星期二很晚,而$dt2
则 <下周五很早。如果您想要日历距离,请使用:
确实,星期二和星期五之间有 3 天。 日历距离 1 表示“明天”,距离 -1 表示“昨天”。 DateTime 对象的“时间”部分大多是无关紧要的(除非两个日期落在不同的时区,那么您必须决定这两个日期之间的“日历距离”应该意味着什么)。
如果您想要绝对距离,请改为使用:
事实上,如果您想将两个日期之间的时间分成 24 小时块,则两者之间只有大约 2.07 天他们。 根据您的应用程序,您可能需要截断或舍入此数字。 DateTime 对象的“时间”部分非常相关,即使对于不同时区的日期,预期结果也得到了很好的定义。
There seems to be quite a bit of confusion because, depending on what you are trying to accomplish, “the number of days between two dates” can mean at least two different things:
As an example and to note the difference, assume that you have two DateTime objects constructed as follows:
Note that
$dt1
is quite late on a Tuesday, while$dt2
is very early on the following Friday.If you want the calendar distance use:
Indeed, between, Tuesday and Friday there are 3 days. A calendar distance of 1 means “tomorrow” and a distance of -1 means “yesterday”. The “time” part of the DateTime objects is mostly irrelevant (except perhaps if the two dates fall on different time zones, then you would have to decide what “the calendar distance” between those two dates should mean).
If you want the absolute distance then instead use:
Indeed, if you want to split the time between the two dates in 24-hour chunks, there are only about 2.07 days between them. Depending on your application, you might want to truncate or round this number. The “time” part of the DateTime objects is very relevant, and the expected result is well defined even for dates on different time zones.
如果您关心准确性,请记住并非所有日子都有 86400 秒。 对于某些情况,基于该假设的任何解决方案都是不正确的。
这是我保留的一个片段,用于使用 DateTime 图书馆。 我想最后打印的答案就是你想要的。
If you care about accuracy, keep in mind that not all days have 86400 seconds. Any solution based on that assumption will not be correct for some cases.
Here's a snippet I keep around to calculate and display date/time differences a few different ways using the DateTime library. The last answer printed is the one you want, I think.
Time::ParseDate 将很好地处理该格式:
Time::ParseDate will handle that format just fine:
Date::Calc 有 Decode_Date_EU (和美国等)
Date::Calc has Decode_Date_EU (and US etc)
这个问题已经有一个很好的答案,但我想提供一个答案,说明为什么计算秒数差异是错误(当我们使用格式化/本地日期而不是浮动日期时)。
我发现有很多建议告诉人们减少秒数,这令人痛苦。 (这个问题是我的搜索中第一个 Google 搜索结果,所以我不在乎它有多久了。)
我自己也犯过这个错误,并想知道为什么应用程序会突然(在周末)显示不正确的时间。 所以我希望这段代码能够帮助人们(可能面临这样的问题)理解为什么这种方法是错误的,并帮助他们避免这种错误。
这是一个完整的示例,在某个关键点不包含“...”(因为如果您在同一时区插入两个日期,您可能不会看到错误)。
输出:
注意:
引用文档(delta_days()与subtract_datetime()):
底线:如果您使用 DateTime,请不要区分秒。 如果您不确定使用什么日期框架,请使用 DateTime,它很棒。
This question already has a nice answer, but I want to provide a answer showing why calculating the difference in seconds is WRONG (when we're using formatted/local dates rather than floating dates).
I find it distressing how many suggestions tell people to subtract seconds. (This question was the first Google hit for my search, so I don't care how old it is.)
I've made that mistake myself and wondered why the application would suddenly (over the weekend) show incorrent times. So I'm hoping this code will help people (who may be facing such an issue) understand why this approach is wrong and help them avoid that mistake.
Here is a complete example, one that doesn't contain "..." at some crucial point (because if you insert two dates in the same time zone, you may not see an error).
Output:
Notes:
Quoting the documentation (delta_days() vs subtract_datetime()):
Bottom line: Don't diff seconds if you're using DateTime. If you're not sure what date framework to use, use DateTime, it's awesome.
您可以将日期转换为长整数格式,即自纪元以来的秒数(我认为是 1970 年的某个日期)。 然后你有两个变量,它们是以秒为单位的日期; 从较大的值中减去较小的值。 现在你的时间跨度以秒为单位; 将其除以 24 小时内的秒数。
You could convert the dates into the long integer format, which is the number of seconds since the epoch (some date in 1970 I think). You then have two variables that are the dates in seconds; subtract the smaller from the larger. Now you have a time span in seconds; divide it by the number of seconds in 24 hours.
将两个日期转换为秒,然后进行数学计算:
Convert the two dates to seconds and then do the math: