[英]Moment.js - Get all mondays between a date range
I have a date range that looks like this 我的日期范围看起来像这样
let start = moment(this.absence.FromDate);
let end = moment(this.absence.ToDate);
The user can decide to deactivate specific week days during that date range, so I have booleans 用户可以决定在该日期范围内停用特定的工作日,因此我有布尔值
monday = true;
tuesday = false;
...
I want to create a function that allows me to put all mondays during my date range in an array. 我想创建一个函数,允许我将日期范围内的所有星期一放在数组中。
I've looked around on stack but I can only find help for people who need all the monday from a month for example. 我在堆栈上四处看看,但我只能找到一个月需要所有星期一的人的帮助。
You can get next Monday using .day(1)
and then loop until your date isBefore
your end
date adding 7 days for each iteration using add
你可以使用下星期一.day(1)
然后循环,直到你的约会isBefore
您的end
日期加7天,使用每次迭代add
Here a live sample: 这是一个实时样本:
//let start = moment(this.absence.FromDate); //let end = moment(this.absence.ToDate); // Test values let start = moment(); let end = moment().add(45 , 'd'); var arr = []; // Get "next" monday let tmp = start.clone().day(1); if( tmp.isAfter(start, 'd') ){ arr.push(tmp.format('YYYY-MM-DD')); } while( tmp.isBefore(end) ){ tmp.add(7, 'days'); arr.push(tmp.format('YYYY-MM-DD')); } console.log(arr);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.