Click here to Skip to main content
14,426,461 members
Rate this:
Please Sign up or sign in to vote.
See more:
hi , is there any possibility to validate date and time with two date time pickers in vb.net coding

EDIT


Dim date1 As Date = Me.DateTimePicker2.Value
        Dim date2 As Date = Me.DateTimePicker3.Value

        If (Me.DateTimePicker3.Value <= Me.DateTimePicker2.Value) Then
            TextBox9.Text = "ON-TIME"
        ElseIf (Me.DateTimePicker3.Value > Me.DateTimePicker2.Value) Then
            TextBox9.Text = "DELAY"
        End If



here i validate the date and display the status in the textbox now by setting the datetimepicker customformat to "MMM dd, yyyy hh:mm:ss tt " i need to validate the time also....
Posted
Updated 28-Nov-11 20:53pm
v2
Comments
   
Can you give an example of the invalid? How can it appear?
--SA
ANANTH P 29-Nov-11 0:44am
   
Dim date1 As Date = Me.DateTimePicker2.Value
Dim date2 As Date = Me.DateTimePicker3.Value

If (Me.DateTimePicker3.Value <= Me.DateTimePicker2.Value) Then
TextBox9.Text = "ON-TIME"
ElseIf (Me.DateTimePicker3.Value > Me.DateTimePicker2.Value) Then
TextBox9.Text = "DELAY"
End If


here i validate the date and display the status in the textbox now by setting the datetimepicker customformat to "MMM dd, yyyy hh:mm:ss tt " i need to validate the time also....
   
OK, this is the way, so what's the problem. Perhaps, this: there is not such thing as date vs. time. There is only on thing: time. A data is nothing more that a position of the time point on the calendar. In your code above, you compare time. A time is a unique point on time scale, and this is what described by the structure DateTime. If your problem is different, please explain.
--SA
Rate this:
Please Sign up or sign in to vote.

Solution 1

The time entered with a date-time picker is always valid. There is nothing to validate.

[EDIT]

Please also see the discussion in comments to the question above. I try to explain the meaning of DateTime. This is not date + time, this is actually time, a unique point on the time line. Comparison of the values ('==', '!=', '<=', '>=', '>', '<') compares which events are simultaneous or which one follows which one; without data or time of the day concerns. This is what should be used for validation of time data.

—SA
   
v2
Comments
Manoj K Bhoir 29-Nov-11 0:45am
   
I agree with AKryukov!Hence 5P's
   
Thank you, Manoj.
--SA
Rate this:
Please Sign up or sign in to vote.

Solution 2

Whatever code provided you above in that you are compairing two Date values with each other.For that you can use :
Dim d1 As DateTime = New DateTime(Year, Month, Day, Hour, Minute, Second)
Dim d2 As DateTime = New DateTime(Year, Month, Day, Hour, Minute, Second)
MessageBox.Show(DateTime.Compare(d1, d2))

It return -1 if d1 is less than d2,1 if d1 is greater than d2 and 0 if both are equal.
   

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




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