javascript 以dd/MM/yyyy格式检查当前日期是否不小于当前日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29445909/
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
To check whether current date is not less than present date in dd/MM/yyyy format
提问by
I am working on JavaScript validation where I am validating whether a textbox
date is equal to the current date or not.
我正在处理 JavaScript 验证,我正在验证textbox
日期是否等于当前日期。
If its greater or equal to today's date than do something, if its less than today's date then show error message.
如果它大于或等于今天的日期而不做某事,如果它小于今天的日期则显示错误消息。
Note: In my textbox
I have converted date into dd/MM/yyyy
format. So I need to check textbox
date with current date in dd/MM/yyy
format only. Here is my code:
注意:在我的textbox
我已将日期转换为dd/MM/yyyy
格式。所以我只需要以textbox
当前日期的dd/MM/yyy
格式检查日期。这是我的代码:
function ValidateDate() {
var EffectiveDate = $.trim($("[id$='txtFromDate']").val());
var Today = new Date();
if(EffectiveDate<Today())
{
//Show Error Message
}
else
{
//Do something else
}
I need the date to be in dd/MM/yyyy
format for checking my textbox
date, so my Today
value has to be in dd/MM/yyyy
format only.
我需要日期的dd/MM/yyyy
格式来检查我的textbox
日期,所以我的Today
值dd/MM/yyyy
只能是格式。
采纳答案by Senthil Rajan
First, we have to find the current date -- below is code that finds it. Then, compare the result with the value entered in TextBox.
首先,我们必须找到当前日期——下面是找到它的代码。然后,将结果与在 TextBox 中输入的值进行比较。
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
if(dd < 10) {
dd = '0' + dd
}
if(mm < 10) {
mm = '0' + mm
}
today = mm + '/' + dd + '/' + yyyy;
document.write(today);
var EffectiveDate = $.trim($("[id$='txtFromDate']").val());
回答by Senthil Rajan
I think it will help for you
我认为它会对你有所帮助
var getdate = new Date($("[id$='txtFromDate']").val());
var curDate = new Date();
alert(getdate - curDate === 0);
alert(getdate - curDate < 0);
alert(getdate - curDate > 0);
回答by Senthil Rajan
function ValidateAddNewCourseCharge() {
var EffectiveDate = $.trim($("[id$='txtFromDate']").val());
var Today = new Date();
var dd = Today.getDate();
var mm = Today.getMonth() + 1; //January is 0!
var yyyy = Today.getFullYear();
if (dd < 10) {
dd = '0' + dd
}
if (mm < 10) {
mm = '0' + mm
}
var Today = dd + '/' + mm + '/' + yyyy;
dateFirst = EffectiveDate.split('/');
dateSecond = Today.split('/');
var value = new Date(dateFirst[2], dateFirst[1], dateFirst[0]);
var current = new Date(dateSecond[2], dateSecond[1], dateSecond[0]);
if (EffectiveDate == "") {
showErrorMessagePopUp("Please Select a Date for Course Charge!");
return false;
}
else {
if (value < current) {
showErrorMessagePopUp("Date should not be less than Present Date!");
return false;
}
}
return true;
}