Answered by:
Comparing Dates Issue

Question
-
User-691412093 posted
Hi guys
I have this issue when I add date information, but first, let me explain what I need.
The user register his work schedule on a single day( example: 01/01/2010 10:00 to 01/01/2010 18:00 and rest from 14:00 to 15:00 on the same), Now, I have a validation on my code that the rest hour can't be after or before his work hours and it runs perfect.
My problem is, if the work schedule is for example from 01/01/2010 22:00 and finish 4:00 the next day and rest 01:00 to 02:00, my validation rejects this because it says that the work schedule is 01/01/2010 22:00 to 01/01/2010 4:00 and the rest hours are rejected.
How I can Fix my validation?
Thank you
Thursday, June 3, 2010 7:05 PM
Answers
-
User-952121411 posted
If restfinish < workfinish Or (restfinish = workfinish) ThenIn the line above did you mean the following:
If (restfinish > workfinish) Or (restfinish = workfinish)
I believe you would want to see if the 'restfinish' is greater than or equal to the finish work time to make sure it is within the bounds. When working with 'DataTime' objects, if the actual Date is part of the variable (i.e. workfinish = "06/08/2010 22:00") then your code will work. However if you are strictly working with times only then the 'Convert.DateTime' method will use the current date as the date portion of the variable. This will obviously cause your issue. You need to do 1 of the following:1. Make the actual date part of the variable that gets created
2. Use logic that if (endtime < starttime) (i.e. 04:00 < 22:00) then we know the endtime is +1 day. However this gets tricky because then you can't have any validation rules that check to see if the end time is before the start time.
Your best option is probably a calendar control + a time selection on entry. The (2) values together should make your code usable.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, June 7, 2010 8:47 AM
All replies
-
User896700311 posted
Hi,
Please, send us your code to correct it.
Regards,
Thursday, June 3, 2010 11:06 PM -
User-691412093 posted
Dim restbegin = Convert.ToDateTime("" & TextStarTimeHourRest.Text & ":" & TextStarTimeMinuteRest.Text & "")Dim restfinish = Convert.ToDateTime("" & TextEndTimeHourRest.Text & ":" & TextEndTimeMinuteRest.Text & "")Dim workbegin = Convert.ToDateTime("" & TextStarTimeHourWork.Text & ":" & TextStarTimeMinuteWork.Text & "")Dim workfinish = Convert.ToDateTime("" & TextEndTimeHourWork.Text & ":" & TextEndTimeMinuteWork.Text & "")If restbegin < workbegin Or (restbegin = workbegin) ThenLabel1.Text = "Rest Time must be within Work time range."End IfIf restfinish < workfinish Or (restfinish = workfinish) ThenLabel1.Text = "Rest Time must be within Work time range."End IfThank you for your attention Segundo, Here is the code:
Dim restbegin = Convert.ToDateTime("" & TextStarTimeHourRest.Text & ":" & TextStarTimeMinuteRest.Text & "")
Dim restfinish = Convert.ToDateTime("" & TextEndTimeHourRest.Text & ":" & TextEndTimeMinuteRest.Text & "")
Dim workbegin = Convert.ToDateTime("" & TextStarTimeHourWork.Text & ":" & TextStarTimeMinuteWork.Text & "")
Dim workfinish = Convert.ToDateTime("" & TextEndTimeHourWork.Text & ":" & TextEndTimeMinuteWork.Text & "")
If restbegin < workbegin Or (restbegin = workbegin) Then
Label1.Text = "Rest Time must be within Work time range."
End If
If restfinish < workfinish Or (restfinish = workfinish) Then
Label1.Text = "Rest Time must be within Work time range."
End If
Friday, June 4, 2010 11:40 AM -
User-952121411 posted
If restfinish < workfinish Or (restfinish = workfinish) ThenIn the line above did you mean the following:
If (restfinish > workfinish) Or (restfinish = workfinish)
I believe you would want to see if the 'restfinish' is greater than or equal to the finish work time to make sure it is within the bounds. When working with 'DataTime' objects, if the actual Date is part of the variable (i.e. workfinish = "06/08/2010 22:00") then your code will work. However if you are strictly working with times only then the 'Convert.DateTime' method will use the current date as the date portion of the variable. This will obviously cause your issue. You need to do 1 of the following:1. Make the actual date part of the variable that gets created
2. Use logic that if (endtime < starttime) (i.e. 04:00 < 22:00) then we know the endtime is +1 day. However this gets tricky because then you can't have any validation rules that check to see if the end time is before the start time.
Your best option is probably a calendar control + a time selection on entry. The (2) values together should make your code usable.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, June 7, 2010 8:47 AM -
User-2079636174 posted
people don't use ORELSE and ANDALSO enough.
Wednesday, June 16, 2010 3:47 AM -
User-952121411 posted
The 'ORELSE' could short circuit checking the second expression if the first expression evaluated to 'True', but other than that it does not refine or improve the code all that much. The 'ANDALSO' short-circuit operator is extremely useful when comparing (2) expressions especially in the case where the second expression should not or can not be evaluated unless the 1st is true (i.e. comparing an object that may not have value, etc). However that use is not applicable to the OP's question. It's not that the operators are not used enough, but more rather developers know that they exist.
Thursday, June 24, 2010 9:46 AM