列出两个日期之间的天数数组 [英] List Array of Days Between Two Dates

查看:37
本文介绍了列出两个日期之间的天数数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想列出两个日期之间的天数数组.我可以用下面的代码列出一系列月份.我该如何更改它以显示两个日期之间的每一天?

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

推荐答案

我不知道你说的是哪一天,所以我已经展示了所有方法.

I don't know which day you meant, thus I have shown all the ways.

 (date_from..date_to).map(&:wday)

#mday 是一个月中的第几天 (1-31).

#mday is the day of the month (1-31).

(date_from..date_to).map(&:mday)

#yday 是一年中的第几天 (1-366).

#yday is the day of the year (1-366).

(date_from..date_to).map(&:yday)

我不太清楚 OP 的实际需求.经过我们之间的一些评论,我从OP的评论中了解到,OP 正在寻找以下答案 -

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 -

(date_from..date_to).map(&:to_s)

这篇关于列出两个日期之间的天数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆