在 Perl 中循环两个日期的最佳方式是什么? [英] What is the optimal way to loop between two dates in Perl?

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

问题描述

在 Perl 中循环两个日期的最佳/最清晰的方法是什么?CPAN 上有很多模块可以处理此类问题,但是是否有任何经验法则可以在两个日期之间进行迭代?

What is the optimal/clearest way to loop between two dates in Perl? There are plenty of modules on CPAN that deal with such matter, but is there any rule of thumb for iterating between two dates?

推荐答案

对于所有使用日期操作的东西,DateTime 可能是最好的模块.要使用您自己的增量获取两个日期之间的所有日期,请使用以下内容:

For everything that uses Date manipulation DateTime is probably the best module out there. To get all dates between two dates with your own increment use something like this:

#!/usr/bin/env perl
use strict;
use warnings;
use DateTime;

my $start = DateTime->new(
    day   => 1,
    month => 1,
    year  => 2000,
);

my $stop = DateTime->new(
    day   => 10,
    month => 1,
    year  => 2000,
);


while ( $start->add(days => 1) < $stop ) {
    printf "Date: %s
", $start->ymd('-');
}

这将输出:

Date: 2000-01-02
Date: 2000-01-03
Date: 2000-01-04
Date: 2000-01-05
Date: 2000-01-06
Date: 2000-01-07
Date: 2000-01-08
Date: 2000-01-09

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

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