在下面的代码中,我想验证 02/08/2017 到 05/08/2017 之间的日期,并向我们发出日期不在范围内的警报
<script type="text/javascript">
function validate()
{
today = new Date();
fromdt= new Date("02/08/2017");
todate=new Date("05/08/2017");
if( document.myForm.entrydt.value == "" )
{
alert( "Please Select Entry Date!" );
document.myForm.entrydt.focus() ;
return false;
}
else if(!document.myForm.entrydt.value.match(letters3))
{
alert("Entry Date: Enter Only Date Format i.e DD/MM/YYYY");
document.myForm.entrydt.focus() ;
return false;
}
else if (!document.myForm.entrydt.value.today > startdt && !document.myForm.entrydt.value.today < todate)
{
alert("Entry Date: Enter Date in Proper Range");
document.myForm.entrydt.focus() ;
return false;
}
return( true );
}
</script>
================================================== =================================
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" class="form-inline" role="form" name="myForm" id="myForm" onsubmit="return(validate());">
<div id="" class="container" >
<div class="form-group">
<label for="entrydt">Entry Date</label>=
<input class="form-control" style='font-weight:bold;text-transform:uppercase;' id="entrydt" type="text" name="entrydt" style='' placeholder="DD/MM/YYYY" value="" size="10">
</div>
</div>
</form>
最佳答案
new Date().getTime() 将给出以毫秒为单位的时间作为长值数字来比较值
var today = new Date().getTime(); // 1501653935994
var from = new Date("02/08/2017").getTime(); // gives 1486492200000
var to = new Date("05/08/2017").getTime();
if(today >= from && today <= to) {
// your code goes here
}
关于javascript - 如何在 Javascript 中检查日期范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45452456/