Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If Time is > < Statement 1

Status
Not open for further replies.

werD420

Technical User
Sep 14, 2004
181
US
Hey all whats a good way to do this


Code:
If Time() >= "6:00:00 AM" and Time() <= "11:00:00 AM" Then 
shift="1" 
ElseIf Time() > "11:00:00 AM" and Time() <= "2:00:00 PM" Then 
shift="2" 
ElseIf Time() > "2:00:00 PM" and Time() <= "5:00:00 PM" Then 
shift="3" 
ElseIf Time() > "5:00:00 PM" and Time() <= "10:00:00 PM" Then
shift="4"
 End If

<% ASP Web App Development %>
 
got this from experts exchange
Code:
theHr = Hour(Time) ' Hour uses military 24 clock
If theHr >= 6 and theHr <= 11 Then 
shift="1" 
ElseIf theHr > 11 and theHr <= 14 Then 
shift="2" 
ElseIf theHr > 14 and theHr <= 17 Then 
shift="3" 
ElseIf theHr > 17 and theHr <= 23 Then
shift="4"
End If
Response.Write(shift &"/")

<% ASP Web App Development %>
 
You might want an ELSE statement to catch the error condition of a time between midnight and 6am... especially since an unitialized date defaults to midnight.
 
Unfortunatly the above statement does not match what your looking for. Try it with 11:01 to see what I mean.

Rather than depend entirely on the hour for determination, you can use dates provided you convert the strings in your original post to dates with either cDate or #'s:
Code:
If Time() >= cDate("6:00:00 AM") and Time() <= cDate("11:00:00 AM") Then
shift="1"
ElseIf Time() > cDate("11:00:00 AM") and Time() <= cDate("2:00:00 PM") Then
shift="2"
ElseIf Time() > cDate("2:00:00 PM") and Time() <= cDate("5:00:00 PM") Then
shift="3"
ElseIf Time() > cDate("5:00:00 PM") and Time() <= cDate("10:00:00 PM") Then
shift="4"
 End If

[b]or[/b]
If Time() >= #6:00:00 AM# and Time() <= #11:00:00 AM# Then
shift="1"
ElseIf Time() > #11:00:00 AM# and Time() <= #2:00:00 PM# Then
shift="2"
ElseIf Time() > #2:00:00 PM# and Time() <= #5:00:00 PM# Then
shift="3"
ElseIf Time() > #5:00:00 PM# and Time() <= #10:00:00 PM# Then
shift="4"
 End If

However, a better way tro do this would be to turn it into a case statement, which will be more efficient and likely easier to read. To get around VBScripts incapability to do relative comparisons in Case statements we can use the Select Case True hack to compare a conditional in a Case to True:
Code:
Select Case True
   Case Time() < #6:00:00 AM#
      shift = -1 'set to -1 as a sign that this is not a valid shift
   Case Time() < #11:00:00 AM#
      shift = 1 'first shift
   Case Time() < #2:00:00 PM#
      shift = 2 'second shift
   Case Time() < #5:00:00 PM#
      shift = 3 'third shift
   Case Time() < #10:00:00 PM#
      shift = 4 'fourth shift
   Case Else
      shift = -1 'this is not a valid shift
End Select

That makes the decision structure a little easier to read as well as covering us in the case that a time outside of the shift schedule is selected. Hope this helps,

-T

barcode_1.gif
 
Figured I hadn't posted that one in a year or two, it needed some attention ;)

barcode_1.gif
 

Nice one !

A smile is worth a thousand kind words. So smile, it's easy! :-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top