I would like to list an array of days between two dates. I can list an array of months with the code below. How might I change this to show every day between two dates?
我想在两个日期之间列出一组日期。我可以用下面的代码列出一个月的数组。我怎样才能在两次约会之间每天都显示出来呢?
require 'date'
date_from = Date.parse('2011-05-14')
date_to = Date.parse('2011-05-30')
date_range = date_from..date_to
date_months = date_range.map {|d| Date.new(d.year, d.month, 1) }.uniq
date_months.map {|d| d.strftime "%d/%m/%Y" }
puts date_months
1 个解决方案
#1
14
I don't know which day you meant, thus I have shown all the ways.
我不知道你指的是哪一天,所以我已经说明了所有的方法。
#wday is the day of week (0-6, Sunday is zero).
(date_from..date_to).map(&:wday)
#mday is the day of the month (1-31).
(date_from..date_to).map(&:mday)
#yday is the day of the year (1-366).
(date_from..date_to).map(&:yday)
OP's actual need was not much clear to me. After few comments between us, I came to know from OP's comment, the below answer OP is looking for -
OP的实际需求我不是很清楚。经过我们之间的几番评论,我从OP的评论中了解到,下面的答案正在寻找。
(date_from..date_to).map(&:to_s)
#1
14
I don't know which day you meant, thus I have shown all the ways.
我不知道你指的是哪一天,所以我已经说明了所有的方法。
#wday is the day of week (0-6, Sunday is zero).
(date_from..date_to).map(&:wday)
#mday is the day of the month (1-31).
(date_from..date_to).map(&:mday)
#yday is the day of the year (1-366).
(date_from..date_to).map(&:yday)
OP's actual need was not much clear to me. After few comments between us, I came to know from OP's comment, the below answer OP is looking for -
OP的实际需求我不是很清楚。经过我们之间的几番评论,我从OP的评论中了解到,下面的答案正在寻找。
(date_from..date_to).map(&:to_s)