[英]js datepicker - allow non-consecutive days pcm
I would like to limit users to selecting only the first and third Monday of each month. 我想限制用户只能选择每月的第一个和第三个星期一。 We have a volunteer intake only on these days, so I want to limit incorrect date selections as much as possible.
这些天我们只有志愿者参加,所以我想尽可能地限制错误的日期选择。
I'm not a js coder, but have managed to adapt some code I found online to allow the first or third Monday of each month, but I can't work out how to allow both of them. 我不是JS编码人员,但是设法修改了一些我在网上找到的代码,以允许每个月的第一个或第三个星期一,但是我不知道如何允许这两个代码。
Here's the code I have for the first Monday: 这是我第一个星期一的代码:
var firstMonday = new Date(date);
var mondays=0;
firstMonday.setDate(1);
while (mondays < 1) {
firstMonday.setDate(firstMonday.getDate() + 1);
if (firstMonday.getDay() == 1) {
mondays++;
}
}
var result = date.getDate() != firstMonday.getDate();
I think this is what you are asking. 我想这就是你要问的。 Credit to jabclab for the getMondays() function.
感谢jabclab获得getMondays()函数。
// test: first monday of this month // result: true //var dates = [new Date(2017,8,4)]; // test: third monday of this month // result: true //var dates = [new Date(2017,8,18)]; // test: first and third monday of this month // result: true var dates = [new Date(2017,8,4), new Date(2017,8,18)]; // test: first monday, third monday, and random day from this month // result: false //var dates = [new Date(2017,8,4), new Date(2017,8,18), new Date(2017,8,22)]; alert(validate(dates)); function validate(dates) { var valid = true; var mondays = getMondays(); var firstMonday = mondays[0].setHours(0,0,0,0); var thirdMonday = mondays[2].setHours(0,0,0,0); if (dates && dates.length > 0) { for (var i = 0; i < dates.length; i++) { // Zero out time so only year, month, and day is compared var d = dates[i].setHours(0,0,0,0); if (d != firstMonday && d != thirdMonday) { return false; } } } else { valid = false; } return valid; } function getMondays() { var d = new Date(), month = d.getMonth(), mondays = []; d.setDate(1); // Get the first Monday in the month while (d.getDay() !== 1) { d.setDate(d.getDate() + 1); } // Get all the other Mondays in the month while (d.getMonth() === month) { mondays.push(new Date(d.getTime())); d.setDate(d.getDate() + 7); } return mondays; }
Thanks, but I'm not sure if the above works or not as I was looking for a js code answer - I'll leave that for someone else to work out. 谢谢,但是我不确定上面的方法是否有效,因为我在寻找js代码答案-我将其留给其他人解决。
...which I've found in the meantime. 我在此期间发现的... Many thanks to Hugh at Fabrik for the following:
非常感谢Fabrik的Hugh:
var thisdate = new Date(date);
thisdate.setHours(0,0,0,0);
var day = 1; // monday
var nth = 1; // first
var first = new Date(thisdate.getFullYear(), thisdate.getMonth(), 1),
add = (day - first.getDay() + 7) % 7 + (nth - 1) * 7;
first.setDate(1 + add);
nth = 3; // third
var third = new Date(thisdate.getFullYear(), thisdate.getMonth(), 1),
add = (day - third.getDay() + 7) % 7 + (nth - 1) * 7;
third.setDate(1 + add);
//console.log(thisdate + ', ' + first + ', ' + third);
var result = (first.getTime() !== thisdate.getTime()) && (third.getTime() !== thisdate.getTime());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.