如何使用momentJS选择所有相同的工作日?例如:本月的所有星期一。如果当天是该月的第15天,则该月有两个星期一,从该月的0到15日。也许它是某种循环,因为时刻应该返回两个日期 - 一个用于第一个星期一,第二个星期一用于给定范围,依此类推。
或者使用任何其他JavaScript库还有其他方法吗?
答案 0 :(得分:0)
正如你所说,循环可以解决这个问题
month
(您获得的所有日期应该具有相同的月份)
function weekdaysFromMonth (date) {
// all the weekdays previous/next to date should be
// 7 days apart and also have the same month
var month = date.get('month')
// make sure that the start date is 4 weeks before now
// we might go to the previous month but anyway we'll
// get the correct dates by doing more iterations
var start = moment(date).subtract(4 * 7, 'days')
var dates = []
for (var i = 0; i < 10; i += 1) {
start.add(7, 'days')
if (start.get('month') === month) {
dates.push(moment(start))
}
}
return dates
}
document.write(
JSON.stringify(
weekdaysFromMonth(moment()), null, ' '
)
)
&#13;
<script src="http://momentjs.com/downloads/moment.js"></script>
&#13;