简体   繁体   中英

Date counting in Ruby

在Ruby中,有没有一种简单的方法可以计算从YYYY-MM-DD到另一个YYYY-MM-DD的天数并列出?

You can use Date.parse to convert the strings to Date objects and then simply use the two Date objects in a range and call to_a on that range. Ie:

( Date.parse(string1) .. Date.parse(string2) ).to_a
Date.parse('2010-01-01').upto(Date.parse('2010-01-31')) do |day|
  puts day
end

Or:

(Date.parse('2010-01-01')..Date.parse('2010-01-31')).each do |day|
  puts day
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM