admin管理员组文章数量:1356045
I have a form with a date picker, which checks if the selected date is not older than 4 months. But my script doesn't work now, because of a new year (2018).
if ((today.getMonth() + 11) - (date.getMonth() + 11) > 4) {
console.log("test");
}
It doesn't check for the months of 2017. I can't find a solution to fix this, does anyone know how to fix this?
I have a form with a date picker, which checks if the selected date is not older than 4 months. But my script doesn't work now, because of a new year (2018).
if ((today.getMonth() + 11) - (date.getMonth() + 11) > 4) {
console.log("test");
}
It doesn't check for the months of 2017. I can't find a solution to fix this, does anyone know how to fix this?
Share Improve this question edited Jan 2, 2018 at 11:10 can asked Jan 2, 2018 at 10:10 cancan 2142 silver badges15 bronze badges 1- your if conditions are kind of redundant – orangespark Commented Jan 2, 2018 at 10:15
2 Answers
Reset to default 6 if(today - date > 1000/*ms*/ * 60/*s*/ * 60/*min*/ * 24/*h*/ * 30/*days*/ * 3/*months*/)
alert("to old!");
Alternatively:
const fourMonthsAgo = new Date();
fourMonthsAgo.setMonth(fourMonthsAgo.getMonth() - 4);
if(+fourMonthsAgo > +date)
alert("to old");
Just pare like this;
var diff =(today.getTime() - date.getTime()) / 1000;
diff = diff / (60 * 60 * 24 * 10 * 3);
var diffMonths = Math.abs(Math.round(diff));
if(diffMonths > 4)
{
var $errordate = $( "<div id='error-field' class='error-field'><p>Error text</p></div>" );
$("#divContainer").append($errordate);
$('#nextButton').prop('disabled', true);
}
Demo
本文标签: javascriptCheck if form date is older than 4 monthsStack Overflow
版权声明:本文标题:javascript - Check if form date is older than 4 months - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744055834a2583234.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论