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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:33:19  来源:igfitidea点击:

To check whether current date is not less than present date in dd/MM/yyyy format

javascriptjquery

提问by

I am working on JavaScript validation where I am validating whether a textboxdate 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 textboxI have converted date into dd/MM/yyyyformat. So I need to check textboxdate with current date in dd/MM/yyyformat 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/yyyyformat for checking my textboxdate, so my Todayvalue has to be in dd/MM/yyyyformat only.

我需要日期的dd/MM/yyyy格式来检查我的textbox日期,所以我的Todaydd/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;
}