检查日期是否在过去 Javascript

IT技术 javascript jquery jquery-ui validation date
2021-03-15 22:49:02

所有,我使用 jQuery UI 作为日期选择器。我正在尝试使用 javascript 进行检查,尽管用户输入的日期是过去的日期。这是我的表单代码:

<input type="text" id="datepicker" name="event_date" class="datepicker">

那么我将如何使用 Javascript 进行检查以确保它不是过去的日期?谢谢

5个回答

$('#datepicker').datepicker().change(evt => {
  var selectedDate = $('#datepicker').datepicker('getDate');
  var now = new Date();
  now.setHours(0,0,0,0);
  if (selectedDate < now) {
    console.log("Selected date is in the past");
  } else {
    console.log("Selected date is NOT in the past");
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<input type="text" id="datepicker" name="event_date" class="datepicker">

您可能可以跳过该now变量。通过使用(selectedDate.getTime() < Date.now())
2021-04-27 22:49:02
@MiguelTrias:当然。这使读者更清楚一点。
2021-05-03 22:49:02
如果您只是查看日期(没有时间),那么您可能应该像这样与今天的开始进行比较 var now = ( new Date() ).setHours(0,0,0,0);
2021-05-03 22:49:02
@IvanYurchenko:.val()返回一个字符串,字面意思是<input>. .datepicker('getDate')返回一个Date对象。
2021-05-07 22:49:02
单线:if ($('#datepicker').datepicker('getDate') < new Date) { ... }使用new关键字调用函数时可以省略括号
2021-05-14 22:49:02
var datep = $('#datepicker').val();

if(Date.parse(datep)-Date.parse(new Date())<0)
{
   // do something
}

为了使答案更可重用于日期选择器更改功能以外的其他内容,您可以创建一个原型来为您处理此问题。

// safety check to see if the prototype name is already defined
Function.prototype.method = function (name, func) {
    if (!this.prototype[name]) {
        this.prototype[name] = func;
        return this;
    }
};
Date.method('inPast', function () {
    return this < new Date($.now());// the $.now() requires jQuery
});

// including this prototype as using in example
Date.method('addDays', function (days) {
    var date = new Date(this);
    date.setDate(date.getDate() + (days));    
    return date;
});

如果你不喜欢安全检查,你可以使用传统的方式来定义原型:

Date.prototype.inPast = function(){
    return this < new Date($.now());// the $.now() requires jQuery
}

示例用法

var dt = new Date($.now());
var yesterday = dt.addDays(-1);
var tomorrow = dt.addDays(1);
console.log('Yesterday: ' + yesterday.inPast());
console.log('Tomorrow: ' + tomorrow.inPast());

只需将日期转换为毫秒并减去

let givenDate1 =   new Date("10/21/2001")  // Past Date
let givenDate2 =   new Date("10/21/2050") // future Date

如果 diff 为,则给定日期为PAST

 let diff = new Date().getTime() - givenDate1.getTime();
  if (diff > 0) {
     console.log('Given Date givenDate1 is in Past');
   }

如果 diff 为,则给定日期为Future

 let diff = new Date().getTime() - givenDate2.getTime();
  if (diff < 0) {
     console.log('Given Date givenDate2 is in Future');
   }
function isPrevDate() {
    alert("startDate is " + Startdate);
    if(Startdate.length != 0 && Startdate !='') {
        var start_date = Startdate.split('-');
        alert("Input date: "+ start_date);
        start_date=start_date[1]+"/"+start_date[2]+"/"+start_date[0];
        alert("start date arrray format " + start_date);
        var a = new Date(start_date);
        //alert("The date is a" +a);
        var today = new Date();
        var day = today.getDate();
        var mon = today.getMonth()+1;
        var year = today.getFullYear();
        today = (mon+"/"+day+"/"+year);
        //alert(today);
        var today = new Date(today);
        alert("Today: "+today.getTime());
        alert("a : "+a.getTime());
        if(today.getTime() > a.getTime() )
        {
            alert("Please select Start date in range");
            return false;
        } else {
            return true;
        }
    }
}
var Startdate 来自哪里?
2021-05-20 22:49:02