Javascript 检查日期是否过去而不提交表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27714469/
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-23 00:32:40  来源:igfitidea点击:

Check if date is in the past without submitting form

javascriptjqueryvalidationdate

提问by Alex

I have this code that I want to check if a date is in the past. I want to check it as soon as the date is entered, before form submission.

我有这个代码,我想检查一个日期是否在过去。我想在输入日期后立即检查它,然后再提交表单。

<input id="datepicker" onchange="checkDate()" required class="datepicker-input" type="text" data-date-format="yyyy-mm-dd" >

<script type="text/javascript">
 function checkDate() {
   var selectedDate = document.getElementById('datepicker').value;
   var now = new Date();
   if (selectedDate < now) {
    alert("Date must be in the future");
   }
 }
</script>

This does not work, if I enter a date in the past (e.g. 2014-12-03) it does not display the alert.

这不起作用,如果我输入过去的日期(例如 2014-12-03),它不会显示警报。

回答by Migwell

All you need to do is convert the string produced by the <input>into a Date using the Date constructor new Date("2014-06-12")

您需要做的就是<input>使用 Date 构造函数将产生的字符串转换为 Datenew Date("2014-06-12")

 function checkDate() {
   var selectedText = document.getElementById('datepicker').value;
   var selectedDate = new Date(selectedText);
   var now = new Date();
   if (selectedDate < now) {
    alert("Date must be in the future");
   }
 }
<input id="datepicker" onchange="checkDate()" required class="datepicker-input" type="date" data-date-format="yyyy-mm-dd" >

回答by che-azeh

The date()function returns a string. Try converting both dates to integers first using the Date.parse()function:

date()函数返回一个字符串。首先尝试使用Date.parse()函数将两个日期转换为整数:

<input id="datepicker" onchange="checkDate()" required class="datepicker-input" type="text" data-date-format="yyyy-mm-dd" >

<script type="text/javascript">
   function checkDate() {
       var selectedDate = document.getElementById('datepicker').value;
       var now = new Date();
       var dt1 = Date.parse(now),
       dt2 = Date.parse(selectedDate);
       if (dt2 < dt1) {
            alert("Date must be in the future");
       }
 }
</script>

回答by t.m.

For those who use type="date" (introduced with HTML5) instead of type="text", it can be done this way:

对于那些使用 type="date"(HTML5 引入)而不是 type="text" 的人,可以这样做:

 function checkDate() {
   var date = this.value;
   if(date.valueAsDate <= new Date()) {
       //Date in the past
   } else {
       //Date in the future
   }
}

回答by Mostafa Khedeer

Simple mine function working ...

简单的矿井功能工作...

function check_not_past_date_time(enter_date) {
    var a =new Date(enter_date);
    var now =new Date();
    if(a>now){
        return true;
    }else {
        return false;
    }
}

回答by Orsos Patrick

You can use

您可以使用


_isDate1LowerThanDate2(date1, date2) {
    return new Date(date1) < new Date(date2);
  }

回答by Alex J

Your code is really close, try this instead:

你的代码非常接近,试试这个:

var selectedDate = $('#datepicker').datepicker('getDate');
var now = new Date();
if (selectedDate < now) {
  // selected date is in the past
}