Click here to Skip to main content
12,451,243 members (52,798 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# Windows Application
i am using 2 datetimepicker.

1) any date select from first datetimepicker.

2) then another one date is select from second datetime picker.

my request is: the second datetimepicker selecting date only accept after first date.
how to compare from first date and second date.

EXAMPLE:

i.e) first selected date is 10/10/2011 ok.

then second datetimepicker selecting date accept only after first date i.e 11/10/2011

but can not accept before date that is 09/10/2011.
Posted 16-Oct-11 23:06pm
Updated 16-Oct-11 23:11pm
uspatel30.3K
v2
Comments
Uma Shankar Patel 17-Oct-11 5:12am
   
Retaged from HTML to C#
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
lukeer 17-Oct-11 5:25am
   
5 for ease of use.
Collin Jasnoch 17-Oct-11 11:57am
   
+5 Simple and with MSDN reference :-)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Use Compare validater

<asp:CompareValidator ID="CompareValidator1" runat="server"  ControlToValidate="txtenddate"
ErrorMessage="End Date is not less than Start Date" Operator="GreaterThanEqual" Type="Date" Text="*" ControlToCompare="txtstartdate"></asp:CompareValidator>
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

private void dateTimePicker1_ValueChanged(...)
{
dateTimePicker2.Value = DateTimePicker1.Value.AddDays(1);
}
 
private void dateTimePicker2_ValueChanged(...)
{
       if(dateTimePicker2.Value < DateTimePicker1.Value)
       {
         MessageBox.Show("Cannot be less than previous date");
         dateTimePicker2.Value = DateTimePicker1.Value.AddDays(1);
       }
}

Edit: Please Check my updated solution
  Permalink  
v3
Comments
lukeer 17-Oct-11 5:26am
   
That wouldn't prevent User from selecting an earlier date on dateTimePicker2 afterwards.
Praveen Kullu 17-Oct-11 5:32am
   
@Lukeer Thanks for pointing it out, I have updated the solution
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Or, you can try this: DateTime.Compare[^]

Nice and clean Smile | :) hope this helps

Cheers...
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160826.1 | Last Updated 17 Oct 2011
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100