You must Sign In to post a response.
  • Category: .NET

    How to validate datetime picker "from date" to "To date "

    how to validate date time picker "from date" to "To date " and To date should not be greater than From date . me using date time picker its a custom control developed by someone it not contain "valuechanged event" and close up event". initially this date time picker(custom control) set as empty i have to select by mouse.
  • #691307
    You can make use of the Compare to validtor control for the same. here is a sample code


    asp:CompareValidator
    id="ProgrammaticID"
    ControlToValidate="Programmatic ID of Server Control to Validate"
    ValueToCompare="value"
    ControlToCompare="value"
    Type="DataType"
    Operator="Operator Value"
    ErrorMessage="Message to display in ValidationSummary control"
    Text="Message to display in control"
    ForeColor="value"
    BackColor="value"
    runat="server" >
    </asp:CompareValidator>

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #691317
    for window apps not web apps

  • #691323
    Hello RajniKant,

    Try the below Code:

    private void dateTimePickerHolidayToDate_ValueChanged(object sender, EventArgs e)
    {
    DateTime fromdate = Convert.ToDateTime(dateTimePickerFromDate.Text);
    DateTime todate1 = Convert.ToDateTime(dateTimePickerToDate.Text);
    if (fromdate1 <= todate1)
    {
    TimeSpan daycount = todate1.Subtract(fromdate1);
    int dacount1 = Convert.ToInt32(daycount.Days) + 1;
    txtDay.Text = Convert.ToString(dacount1);
    }
    else
    {
    MessageBox.Show("From Date Must be Less Than To Date");
    }
    }

    Hope it helps you.

    Regards,
    Ankit Jain

  • #691330
    Date checking in Datetime picker is bit easy. as we know datetime picker return Datetime object so we can store them in DateTime object and check DateFrom and DateTo


    DateTime DateFrom = dtPickerFrom.Value;
    DateTime DateTo = dtPickerTo.Value;

    if(DateFrom > DateTo)
    {
    MessageBox.Show("Date from should not be greater than DateTo ");
    }



    hope it helps

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.