Javascript - 日期范围验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13780881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 12:53:12  来源:igfitidea点击:

Javascript - date range validation

javascriptdaterange

提问by Anna Lord

i've a form user can enter any date , but i want to set a date range validation . for example: from 1-12-2012 to 1-1-2013 and the system can't accept any date from user that not in the range.. i've tried this javascript code but it doesn't give me any alert even when date not in range ..

我有一个用户可以输入任何日期的表单,但我想设置一个日期范围验证。例如:从 1-12-2012 到 1-1-2013 并且系统无法接受来自用户的任何不在范围内的日期..我已经尝试过这个 javascript 代码,但它甚至没有给我任何警报当日期不在范围内时..

this is part of my form :

这是我表格的一部分:

 echo "<tr><th bgcolor='FF6600'> Date <font size='4' color='red'>* </font></th>
        <td> <input type='date' name='v2' value=''   ></td></tr>";


echo "<tr><th bgcolor='FF6600'> Time <font size='4' color='red'>* </font></th>
        <td> <input type='time' name='v3' value=''   ></td></tr>";

        echo "<tr><th bgcolor='FF6600'> Place <font size='4' color='red'>* </font></th>
        <td> <input type='text' name='v4' value=''   ></td></tr>";

and this is the javascript code

这是javascript代码

    <script language="javascript">

    function validation(form)
 {

 var v2 = document.getElementById('v2');
 var date = v2.value;
 if ( (v2 > new date('12/12/2012')) && 
    (v2 < new date('1/1/2013')) ){
    // date is in your valid range
     return true;
} else {
    // date is not in your valid range

    alert("date is not in valid range")
    return false;
}


}

    </script>

回答by adeneo

To compare dates, you should be using the unix timestamp, and right now the first date you're getting from the value is a string, and you can't compare that against date objects?

要比较日期,您应该使用 unix 时间戳,而现在您从该值中获得的第一个日期是一个字符串,您无法将其与日期对象进行比较?

Make sure you have timestamps in unix time, and then compare those:

确保您有 unix 时间的时间戳,然后比较它们:

function validation(form) {
    var v2 = document.getElementById('v2'),
      date = new Date(v2.value),
      d1   = date.getTime(),
      d2   = new Date('12/12/2012').getTime(),
      d3   = new Date('1/1/2013').getTime();

   if (d1 > d2 || d1 < d3) {
       return true;
   }else{
       alert("date is not in valid range")
   }
}

回答by Zaheer Ahmed

You can make a function like this:

您可以创建这样的函数:

function checkMyDateWithinRange(myDdate){
    var startDate = new Date(2012, 11, 1);
    var endDate = new Date(2012, 0, 1);     
    if (startDate < myDate && myDate < endDate) {
       return true; 
    }
   return false;
}

and test any date like calling this function:

并测试任何日期,例如调用此函数:

var inputDate= document.getElementById('tbDate'),
date = new Date(inputDate.value);
if(!checkMyDateWithinRange(date)){
    alert('Date is not in range!!!');
}

Here is Working Demo

这是工作演示