我想在 javascript 中检查当前日期是否在 11 月 1 日和 3 月之间,如果是则执行某些操作。
这是我当前的代码:
var today = new Date();
if (today.getMonth() >= 10 && today.getMonth() < 2) {
$(document).ready( function(){
$.fn.test();
});
}
我想在 javascript 中检查当前日期是否在 11 月 1 日和 3 月之间,如果是则执行某些操作。
这是我当前的代码:
var today = new Date();
if (today.getMonth() >= 10 && today.getMonth() < 2) {
$(document).ready( function(){
$.fn.test();
});
}
你可以这样做:
var today = new Date();
if (today.getMonth() >= 10 || today.getMonth() < 2) {
// ... do stuff
}
var d = new Date();
var MARCH_ZERO_INDEXED = 2, NOVEMBER_ZERO_INDEXED = 10;
if (MARCH_ZERO_INDEXED <= d.getMonth() && d.getMonth() < NOVEMBER_ZERO_INDEXED) {
alert(d + " is between 1 Mar and 1 Nov");
} else {
alert(d + " is between 1 Nov and 1 Mar of the next year");
}
或者
var d = new Date();
var year = d.getFullYear();
var nov1 = new Date(year, 10 /* zero-indexed */, 1 /* one-indexed */);
var mar1 = new Date(year, 2, 1);
if (mar1 <= d && d < nov1) {
alert(d + " is between 1 Mar and 1 Nov");
} else {
alert(d + " is between 1 Nov and 1 Mar of the next year");
}
两者都假设您想使用 JS 解释器的时区进行测试。