简体   繁体   English

比较日期值是否相等

[英]comparing date values for equality

I have a text box where user enters date in format "YYYY-MM-DD".我有一个文本框,用户在其中输入格式为“YYYY-MM-DD”的日期。 I have to check whether the entered date is current date as per the server date.我必须根据服务器日期检查输入的日期是否是当前日期。 If both the date matches then only proceed further.如果两个日期都匹配,则仅继续进行。 I have to check only the date not date time.我只需要检查日期而不是日期时间。

<input type="text" class="form-control input_text" runat="server" id="englishDate9"/>

So how can I check this, is it possible to use asp validator or I have to do it from code behind?那么如何检查这一点,是否可以使用 asp 验证器,或者我必须从后面的代码中执行它?

#region date check section                
    string sysDt = englishDate9.Value;
    DateTime oDate = DateTime.Parse(sysDt).Date;
    DateTime sysdate = DateTime.Now.Date;
    if(oDate == sysdate)
    {
        // show message
    }
#endregion

I am using this code, but I am confused is this the correct code or not although for now it is giving correct result for me?我正在使用此代码,但我很困惑这是不是正确的代码,尽管现在它为我提供了正确的结果?

Hope this will help.希望这会有所帮助。

DateTime oDate;
                if(DateTime.TryParse(englishDate9.Value, out oDate))
                {
                    if(oDate != DateTime.Today)
                    {                            
                        return;
                    }
                }
                else
                {                      
                    return;
                }

You should do like this.你应该这样做。 because string any be anything thing other than the valid date and the required format.因为字符串可以是除有效日期和所需格式之外的任何东西。

string dateString = "2009-06-20";
DateTime dt;
bool isValid = DateTime.TryParseExact(dateString, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt);
DateTime sysdate = DateTime.Now.Date;
if (isValid == true && DateTime.Now.Date == sysdate)
{
    //Do something
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM