本文介绍了如何获取两个日期之间的所有星期日/星期一/星期二?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给出开始日期和结束日期,如何获得所有信息,例如9月1日至11月2日是星期天?
Given a start date and an end date, how can I get all the, e.g. Sundays between September 1st and November 2nd?
注意-不是重复:我不希望计数,而是将各个日期作为javascript日期或momentjs对象(答案链接在另一个因此,问题是要求一个 count )
Note - not a duplicate: I don't want a count, but rather the individual dates as either javascript dates or momentjs objects (The answer linked in the other SO question is asking for a count)
推荐答案
使用循环不断获取下一个星期日的日期,直到您结束日期为止.
Use a loop to continually get the date for next Sunday until you pass the ending date.
var start = moment('2016-09-01'), // Sept. 1st
end = moment('2016-11-02'), // Nov. 2nd
day = 0; // Sunday
var result = [];
var current = start.clone();
while (current.day(7 + day).isBefore(end)) {
result.push(current.clone());
}
console.log(result.map(m => m.format('LLLL')));
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
这篇关于如何获取两个日期之间的所有星期日/星期一/星期二?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!