简体   繁体   English

如何在javascript中检查日期不应为当前日期的过去日期?

[英]How to check date should not be past date for current date in javascript?

i am using jquery datepicker.i have to validate that selected date should not be past date from current date like given below 我正在使用jquery datepicker.i必须验证所选日期不应该是当前日期之后的日期,如下所示

from 2015-06-12 to 2015-05-12 从2015-06-12到2015-05-12

how to validate this ... 如何验证这个...

Just use: Date.parse(date) > Date.now() for comparison to now, or Date.parse(comparableDate1) > Date.parse(comparableDate2) for general comparison 只需使用: Date.parse(date) > Date.now()与现在进行比较,或使用Date.parse(comparableDate1) > Date.parse(comparableDate2)进行一般比较

Here's a JSFiddle 这是一个JSFiddle

Here you can find a fiddle example for doing this: http://jsfiddle.net/H3H8s/5/ 在这里您可以找到执行此操作的小提琴示例: http : //jsfiddle.net/H3H8s/5/

Script : 剧本:

if(dateCheck("02/05/2013","02/09/2013","02/07/2013"))
    alert("Availed");
else
    alert("Not Availed");

function dateCheck(from,to,check) {

    var fDate,lDate,cDate;
    fDate = Date.parse(from);
    lDate = Date.parse(to);
    cDate = Date.parse(check);

    if((cDate <= lDate && cDate >= fDate)) {
        return true;
    }
    return false;
}

Since you're using JS Datepicker, you can restrict the min-date to current date. 由于您使用的是JS Datepicker,因此可以将最小日期限制为当前日期。

Here's a JSFiddle 这是一个JSFiddle

$(function(){
var d = new Date();
$('#thedate').datepicker({
    dateFormat: 'dd-mm-yy',
    minDate:"-1d",
    maxDate:"d"
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM