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 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
}
Run Code Online (Sandbox Code Playgroud)
更新(看到您的评论/更新后):
要解析日期字符串,请查看DateTime::Formaton模块CPAN.
这是一个使用DateTime::Format::DateParse它解析YYYY/MM/DD 的示例:
use DateTime::Format::DateParse;
my $d = DateTime::Format::DateParse->parse_datetime( '2010/06/23' );
Run Code Online (Sandbox Code Playgroud)