javascript-日期应为本年11月1日至明年2月10日
我提出了一些条件,日期应该是今年11月1日到明年2月10日 这就是我尝试过的javascript-日期应为本年11月1日至明年2月10日,javascript,date,date-range,Javascript,Date,Date Range,我提出了一些条件,日期应该是今年11月1日到明年2月10日 这就是我尝试过的 if((date.getDate() >= 1 && date.getMonth() === 11 && date.getFullYear()) && (date.getDate() <= 10 && date.getMonth() === 2 && date.getFullYear + 1)){ condition sa
if((date.getDate() >= 1 && date.getMonth() === 11 && date.getFullYear()) && (date.getDate() <= 10 && date.getMonth() === 2 && date.getFullYear + 1)){
condition satisfied
}
如果((date.getDate()>=1&&date.getMonth()==11&&date.getFullYear())&&date.getDate()以下是一个有效的解决方案:
function check(date) {
currentYear = new Date().getFullYear()
return date > new Date(currentYear + '-11-01') && date < new Date(currentYear + 1 + '-02-10')
}
console.log(check(new Date())); // currently false
console.log(check(new Date('2018-12-01'))); // true
console.log(check(new Date('2019-01-31'))); // true
以下是一个有效的解决方案:
function check(date) {
currentYear = new Date().getFullYear()
return date > new Date(currentYear + '-11-01') && date < new Date(currentYear + 1 + '-02-10')
}
console.log(check(new Date())); // currently false
console.log(check(new Date('2018-12-01'))); // true
console.log(check(new Date('2019-01-31'))); // true
代码不起作用,因为在比较日期时,您必须比较整个日期,而不仅仅是部分日期。您可以从日期实例获取当前年份,然后使用它创建要与之比较的限制日期,例如
函数测试日期(日期){
变量年份=新日期().getFullYear();
返回日期>=新日期(1年10月1日)和当前年度11月1日
日期代码不起作用,因为在比较日期时,您必须比较整个日期,而不仅仅是部分日期。您可以从日期实例获取当前年份,然后使用它创建要与之比较的限制日期,例如
函数测试日期(日期){
变量年份=新日期().getFullYear();
返回日期>=新日期(1年10月1日)和当前年度11月1日
日期您可以轻松使用
让checkDifferenceDate=moment()>=moment(“12/01/”+moment().year())&&moment()您可以轻松使用
让checkDifferenceDate=moment()>=moment(“12/01/”+moment().year())和&moment()getMonth()
以0为基础,所以11月是10日,2月是1日。和&date.getFullYear()
没有检查条件,所以总是真实的。您可以考虑使用EPOCH作为EPOCH=new date().getTime()
与var d1=date.UTC进行比较(2018,10,1);
而不是检查多个conditions@AlexK.-如果年份为0000,则不真实;-getMonth()
以0为基础,因此11月是10日,2月是1日。&&date.getFullYear()
没有检查条件,因此始终是真实的。您可以考虑使用EPOCH作为EPOCH=new date().getTime()
与var d1=Date.UTC(2018,10,1)进行比较;
而不是检查多个conditions@AlexK.-如果年份为0000,则不真实;-)请注意新日期('2018-12-01')
将被视为UTC,但getFullYear返回本地年份,因此除非主机时区偏移量为+00:00,否则这将在明显不频繁和随机的时间出错。一个好的答案应该包括解释OP的问题以及代码如何修复它。请注意新日期('2018-12-01')
将被视为UTC,但getFullYear返回本地年份,因此这将在明显不频繁和随机的时间出错,除非主机时区偏移量为+00:00。一个好的答案应该包括对OP问题的解释以及代码如何修复它。
let checkDifferenceDate = moment() >= moment("12/01/"+moment().year()) && moment() <= moment("01/31/"+moment().add(1,'year').year())
if(checkDifferenceDate){
//You logic here !
}