如何遍历日期范围? [英] How to iterate through range of Dates?
本文介绍了如何遍历日期范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的脚本中,我需要遍历给定开始日期和结束日期的一系列日期.我怎样才能在 Perl 中做到这一点?
In my script, I need to iterate through a range of dates given the start date and end date. How can I do this in Perl?
推荐答案
使用 DateTime
模块.这是一个列出前十天的简单示例:
Use DateTime
module. Here is a simple example which lists the ten previous days:
use 5.012;
use warnings;
use DateTime;
my $end = DateTime->now;
my $day = $end->clone->subtract( days => 10 ); # ten days ago
while ($day < $end) {
say $day;
$day->add( days => 1 ); # move along to next day
}
更新(看到您的评论/更新后):
Update (after seeing your comment/update):
要解析日期字符串,请查看 DateTime::Format
在 CPAN 模块上.
To parse in a date string then look at the DateTime::Format
on modules CPAN.
这是一个使用 DateTime::Format::DateParse<的例子/code>
解析 YYYY/MM/DD:
Here is an example using DateTime::Format::DateParse
which does parse YYYY/MM/DD:
use DateTime::Format::DateParse;
my $d = DateTime::Format::DateParse->parse_datetime( '2010/06/23' );
这篇关于如何遍历日期范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文