Javascript jQuery 函数将日期与当前日期进行比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14439471/
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
jQuery function to compare date with current date
提问by Supreet
i am entering date of birth in form by entering date in separate text box ,selecting year from drop down and entering year in text box. Now i want to check that entered date must not be greater that current date in jquery. Please help how?
我通过在单独的文本框中输入日期,从下拉列表中选择年份并在文本框中输入年份来输入出生日期。现在我想检查输入的日期不能大于 jquery 中的当前日期。请帮忙怎么做?
var CurrentMonth = date.getDate() + '/' + date.getMonth() + '/' + date.getFullYear();
var SelectedDate = $('[id$=spDate]').val() + '/' + $('[id$=drpMonth]').val() + '/' + $('[id$=txtYear]').val();
if (CurrentDate > SelectedDate) {
return false;
}
回答by George
Provided that your input values are integers, you can construct a Dateinstance using them:
假设您的输入值是整数,您可以Date使用它们构造一个实例:
var CurrentDate = new Date();
var SelectedDate = new Date(
$('[id$=txtYear]').val(),
$('[id$=drpMonth]').val(),
$('[id$=spDate]').val()
);
//As quite rightly mentioned, January = 0, so if your inputs have the literal number for each month (1 for January) replace the line above with the following, to take a month off:
//var SelectedDate = new Date($('[id$=txtYear]').val(), $('[id$=drpMonth]').val()-1, $('[id$=spDate]').val());
if(CurrentDate > SelectedDate){
//CurrentDate is more than SelectedDate
}
else{
//SelectedDate is more than CurrentDate
}
回答by Alnitak
Comparing Dateobjects is trivial, just use the standard relational operators. When passed an Objectthey implicitly call the object's .valueOf()function which will return the time in milliseconds, suitable for comparison.
比较Date对象很简单,只需使用标准的关系运算符。当传递时,Object它们隐式调用对象的.valueOf()函数,该函数将以毫秒为单位返回时间,适合进行比较。
The hard part is constructing a valid Dateobject in the first place.
困难的部分首先是构建一个有效的Date对象。
For reliability I strongly recommend using this version of the Dateconstructorrather than passing a string:
为了可靠性,我强烈建议使用此版本的Date构造函数而不是传递字符串:
new Date(year, month, day [, hour, minute, second, millisecond]);
which ensures that the local date format doesn't matter. Passing a string can generate different results depending on whether the default date format is mm/dd/yyyyor dd/mm/yyyyin the user's locale.
这确保本地日期格式无关紧要。传递字符串可以生成不同的结果,具体取决于默认日期格式是在用户的语言环境中mm/dd/yyyy还是dd/mm/yyyy在用户的语言环境中。
Don't forget that the year must be a full digit year, and that in this version January is represented by 0, not 1:
不要忘记年份必须是全数字年份,并且在此版本中一月由 0 表示,而不是 1:
var SelectedDate = new Date(
$('[id$=txtYear]').val(),
$('[id$=drpMonth]').val() - 1,
$('[id$=spDate]').val());
if (CurrentDate > SelectedDate) { ... }
回答by srikanthbattula
function compareDate() {
var dateEntered = document.getElementById("txtDateEntered").value;
var date = dateEntered.substring(0, 2);
var month = dateEntered.substring(3, 5);
var year = dateEntered.substring(6, 10);
var dateToCompare = new Date(year, month - 1, date);
var currentDate = new Date();
if (dateToCompare > currentDate) {
alert("Date Entered is greater than Current Date ");
}
else {
alert("Date Entered is lesser than Current Date");
}
}
回答by himanshupareek66
Check if this can help:
检查这是否有帮助:
var TodayDate = new Date();
var endDate= new Date(Date.parse($("#EndDate").val()));
if (endDate> TodayDate) {
// throw error here..
}
回答by Nalan Madheswaran
Try this function:
试试这个功能:
function isFutureDate(dateText) {
var arrDate = dateText.split("/");
var today = new Date();
useDate = new Date(arrDate[2], arrDate[1] - 1, arrDate[0]);
if (useDate > today) {
return true;
} else return false;
}

