Dawood Ahmed
Dawood Ahmed

Reputation: 1764

Compare date with today in asp.net

i have a textbox which takes input date from user. now i want to make a validator which checks either date is greater then today or not.

i tried this link but it has some problems http://forums.asp.net/t/1116715.aspx/1

if i give this date 25/03/2013 it is correct but if give 01/04/2013, it says it is less then today.

**

Update

<asp:CompareValidator ID="CompareValidator2" runat="server" ControlToValidate="txtReturnDate"
                                Display="Dynamic" ErrorMessage="Date should be greater then  today" ForeColor="Red"
                                Operator="GreaterThan" ValidationGroup="VI">Date should be greater then  today</asp:CompareValidator>

**

Please help me to solve this problem

Upvotes: 1

Views: 38188

Answers (5)

Don David
Don David

Reputation: 47

Use below code to compare specified date with todays date

string date = "01/04/2013";
                DateTime myDate = DateTime.ParseExact(date, "dd/MM/yyyy",
                                           System.Globalization.CultureInfo.InvariantCulture);
                if (myDate > DateTime.Today)
                {
                    Console.WriteLine("greater than");
                }
               else
                {
                 Console.WriteLine("Less Than");
                }

Upvotes: 3

Dawood Ahmed
Dawood Ahmed

Reputation: 1764

ok i have done this by

CompareValidator1.ValueToCompare = DateTime.Today.ToString("MM/dd/yyyy");

Upvotes: 2

Jamiec
Jamiec

Reputation: 136104

The problem is that 25/3/2013 is unambiguosly 25th March 2013, however with the wrong culture settings, 01/04/13 could be 4th january 2013 which is indeed before today's date. I assume you thought you were entering 1st April 2013 which would be after.

The solution is one of

  • Use an unambiguous date format when typing into your textbox (2013-01-04 for 1st April)
  • Use a date selector component which exposes the actual date
  • parse the date in the way you expect it (dd/MM/yyyy)

The problem with asp:CompareValidator is that it does not seem to understand that dates can be formatted differently, and uses just the ToShortDateString variant of a DateTime to compare (whoever implemented this should be shot!). The solution according to this question seems to be to use a CustomValidator

protected void DateTimeComparision_ServerValidate(object source, ServerValidateEventArgs args)
{
    args.IsValid = DateTime.ParseExact(txtDate.Text,"dd/MM/yyyy") > DateTime.Today 
}

Upvotes: 1

Surya Deepak
Surya Deepak

Reputation: 431

this kinds of date validations should be permormed on the client side..in my Application we have used the following code

convert: function (d) {
        /* Converts the date in d to a date-object. The input can be:
        a date object: returned without modification
        an array      : Interpreted as [year,month,day]. NOTE: month is 0-11.
        a number     : Interpreted as number of milliseconds
        since 1 Jan 1970 (a timestamp) 
        a string     : Any format supported by the javascript engine, like
        "YYYY/MM/DD", "MM/DD/YYYY", "Jan 31 2009" etc.
        an object     : Interpreted as an object with year, month and date
        attributes.  **NOTE** month is 0-11. */
        return (
        d.constructor === Date ? d :
        d.constructor === Array ? new Date(d[0], d[1], d[2]) :
        d.constructor === Number ? new Date(d) :
        d.constructor === String ? new Date(d) :
        typeof d === "object" ? new Date(d.year, d.month, d.date) :
        NaN
    );

 isFutureDate: function (a) {
        var now = new Date();
        return (a > now) ? true : false;
    },

Now call the above functions like this (isFutureDate(convert("your form date value"))).

Upvotes: 0

Aidan
Aidan

Reputation: 4891

It thinks 1/4/2013 is the 4th January. You should create a DateTime object using the new DateTime(Year, Month, Day) constructor an the comparisson will work correctly, i.e

var compareDate = new DateTime(2013,4,1)
bool afterToday = DateTime.Today < compareDate

Upvotes: 0

Related Questions