json 迭代数天,从 x 日期开始到结束日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15590613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Iterate over days, starting from x date through an end date
提问by calf
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.
例如,我需要从 2013 年 1 月 1 日开始,并为每个日期“做一些事情”,从而为每个日期生成一个 JSON 文件。
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.
我有一个单一日期的“做一些事情”部分,但我很难从一个日期开始并循环到另一个结束日期。
回答by Intrepidd
You can use ranges :
您可以使用范围:
(Date.new(2012, 01, 01)..Date.new(2012, 01, 30)).each do |date|
# Do stuff with date
end
or (see @awendt answer)
或(见@awendt 答案)
Date.new(2012, 01, 01).upto(Date.new(2012, 01, 30)) do |date|
# Do stuff with date
end
回答by awendt
You could use:
你可以使用:
first.upto(last) do |date|
where firstand lastare Date objects.
其中first和last是 Date 对象。
See what I did here in a project of mine, for example.

