我已经对这个问题进行了广泛的研究,并且找到了一些有用的信息,但是还无法解决我的问题。我要做的就是解析一个日期并将其与另一个日期进行比较。看起来很简单,对吧?这是我尝试过的:
function getCurrentDate() { //this function simply returns today's date
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1;
var yyyy = today.getFullYear();
if (dd < 10) {
dd = '0' + dd
}
if (mm < 10) {
mm = '0' + mm
}
today = mm + '/' + dd + '/' + yyyy;
return today;
}
$("#TxtDate").blur(function () {
var projectDueDate = Date.parse($("#lblDueDate").val()); //parses the project due date label to create a date variable
var itemDueDate = new Date($("#TxtDate").val()); //parses the value the user entered into the due date box to create a date variable
var actualProjectDueDate = new Date(projectDueDate);
if (Date.parse(document.getElementById('TxtDate').value) > getCurrentDate()) {
alert("The date you entered precedes today's date. Please enter a valid date.");
$("#TxtDate").val() = "";
}
});
该if
语句在TxtDate
blur
函数中不起作用。即使我输入的日期早于今天,它也不会显示警报窗口。如您所见,我已经尝试了一些不同的方法。有什么建议么?
您的函数getCurrentDate
返回一个string
非a date object
,并将其与进行比较date object
。因此,您需要解析的返回值getCurrentDate
。
if (Date.parse(document.getElementById('TxtDate').value) > Date.parse( getCurrentDate())) {
alert("The date you entered precedes today's date. Please enter a valid date.");
$("#TxtLeaveFrom").val() = "";
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句