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

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

问题描述

我在Perl中想知道在两个日期之间循环的最佳/最清晰的方法是什么?
cpan有很多模块处理它,但是有没有一个经验法则可以在两个日期之间进行迭代?



谢谢,

解决方案

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

 #!/ usr / bin / env perl 
使用严格;
使用警告;
使用DateTime;

我的$ 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){
printfDate:%s\\\
,$ start - > ymd(' - ');
}

这将输出:



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


I was wondering in Perl what is the optimal/clearest way to loop between two dates ? There is plenty modules on cpan that deals with it, but is there any rule of thumb for iterating between two dates ?

Thank you,

解决方案

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\n", $start->ymd('-');
}

This will output:

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天全站免登陆