0

我想在 javascript 中检查当前日期是否在 11 月 1 日和 3 月之间,如果是则执行某些操作。

这是我当前的代码:

var today = new Date();
if (today.getMonth() >= 10 && today.getMonth() < 2) {
    $(document).ready( function(){ 
        $.fn.test(); 
    });
}
4

2 回答 2

2

你可以这样做:

var today = new Date();
if (today.getMonth() >= 10 || today.getMonth() < 2) {
    // ... do stuff
} 
于 2012-12-20T17:29:57.273 回答
0
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 解释器的时区进行测试。

于 2012-12-20T17:27:41.380 回答