不应选择当前日期之前的日期

时间:2011-04-12 13:37:58

标签: javascript

我想在日历中点击当天之前的任何日期时显示弹出消息?这是具有用户单击日期的函数。 setCalendarControlDate(年,月,日)具有用户选择的日期。我如何比较日期并显示错误。

function setCalendarControlDate(year, month, day) {

calendarControl.setDate(year, month, day);
var arguments = "changeDataGrid;day=" + day;
arguments += ";month=" + month;
arguments += ";year=" + year;
//  arguments += ";rowId=" + rowId;
arguments += ";" + rowId;
arguments += ";";

document.getElementById(targetControlId.id).value = arguments;
document.forms[0].submit();

}

1 个答案:

答案 0 :(得分:2)

var selectedDate = new Date(year, month-1, day);

var valid = selectedDate >= new Date();


// If today was 12th April 2011
year = 2011;
month = 4;
day = 13;

var selectedDate = new Date(year, month-1, day);

var valid = selectedDate >= new Date();

valid ; //# => true

year = 2011;
month = 4;
day = 11;

selectedDate = new Date(year, month-1, day);

var valid = selectedDate >= new Date();

valid ; //# => false