从x日期到结束日期进行几天的迭代 [英] Iterate over days, starting from x date through an end date
本文介绍了从x日期到结束日期进行几天的迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,我需要从2013年1月1日开始,为每个日期做一些事情",从而为每个日期生成一个JSON文件.
I need to start from, for example, January 1 2013, and "do some things" for each date, resulting in a JSON file for each date.
我有一个日期完成了做一些事情"部分,但是我很难从一个日期开始,一直循环到另一个结束日期.
I have the "do some things" part worked out for a single date, but I'm having a hard time starting at a date and looping through to another end date.
推荐答案
您可以使用range:
You can use ranges :
(Date.new(2012, 01, 01)..Date.new(2012, 01, 30)).each do |date|
# Do stuff with date
end
或(请参阅@awendt答案)
or (see @awendt answer)
Date.new(2012, 01, 01).upto(Date.new(2012, 01, 30)) do |date|
# Do stuff with date
end
这篇关于从x日期到结束日期进行几天的迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文