Click here to Skip to main content
15,912,977 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi , is there any possibility to validate date and time with two date time pickers in vb.net coding

EDIT


VB
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 19:53pm
v2
Comments
Sergey Alexandrovich Kryukov 29-Nov-11 0:36am    
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....
Sergey Alexandrovich Kryukov 29-Nov-11 0:59am    
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

Whatever code provided you above in that you are compairing two Date values with each other.For that you can use :
VB
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.
 
Share this answer
 
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
 
Share this answer
 
v2
Comments
Manoj K Bhoir 29-Nov-11 0:45am    
I agree with AKryukov!Hence 5P's
Sergey Alexandrovich Kryukov 29-Nov-11 0:54am    
Thank you, Manoj.
--SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900