我的申请有变化。我想限制用户可以在时间跨度内登录到定义的日志的时间。
从AM到PM的转换1工作正常,但PM到AM检查正在产生问题。这是因为第二班从下午5点开始,到第二天上午10点结束。
用户表结构是
shiftid, starttime, endtime
1, 6:00:00 AM, 11:30:00 PM
2, 5:00:00 PM, 10:00:00 AM
答案 0 :(得分:1)
假设starttime和endtime是表中的时间值,您可以使用以下命令:
DECLARE
@timenow TIME
-- Get the current time
SELECT @timenow = CONVERT(TIME, GETDATE())
SELECT *
FROM temp
-- This clause handles the case where the shifts don't go past midnight
WHERE ((endtime > starttime) AND (@timenow BETWEEN starttime AND endtime))
-- This clause handles the case where the shifts do go past midnight
OR ((endtime < starttime) AND (@timenow >= starttime OR @timenow <= endtime))
如果您的值未以Time
的数据类型存储,则可以将它们转换为:
CONVERT(TIME, starttime)
<强>更新强>
要在代码中执行此操作,您需要更改以下行:
If tNow > stTime And tNow < edTime Then
匹配sql中的逻辑:
If tNow >= stTime OrElse tNow <= edTime Then
这使得代码:
Dim tNow As String
Dim stTime As String
Dim edTime As String
tNow = Format(Date.Now, "hh:mm tt")
stTime = Format(ds.Tables("shiftdetails").Rows(0).Item("starttime"), "hh:mm tt")
edTime = Format(ds.Tables("shiftdetails").Rows(0).Item("endtime"), "hh:mm tt")
If stTime < edTime Then
If tNow >= stTime OrElse tNow <= edTime Then
bShift = True
End If
Else
If tNow >= stTime And tNow <= edTime Then
bShift = True
End If
End If
答案 1 :(得分:0)
select .... where timeWork >= TimeStart and timeWork < TimeEnd
and timeStart < timeEnd
or timeWork <= TimeStart and timeWork > TimeEnd
and timeStart < timeEnd