nischalinn
nischalinn

Reputation: 1175

comparing date values for equality

I have a text box where user enters date in format "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?

#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?

Upvotes: 0

Views: 120

Answers (2)

user4221591
user4221591

Reputation: 2180

Hope this will help.

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

Upvotes: 1

Vivek Nuna
Vivek Nuna

Reputation: 1

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
}

Upvotes: 0

Related Questions