4

用户可以在表格中选择开始日期和结束日期。(假设周五到周一。)如何检查当前工作日是否在这两个工作日之间?

Dim WEstart As DayOfWeek = 5 'Friday
Dim WEend As DayOfWeek = 1 'Monday

If Date.Now.DayOfWeek >= WEstart And Date.Now.DayOfWeek <= WEend Then
   Return True
Else
   Return False
End If

不能正常工作。

4

2 回答 2

2

试试看。这将检查星期五和星期一之间的日子。也就是说,它在周五、周六、周日和周一返回“True”。

如果星期几是星期二、星期三或星期四,则返回 false。

Dim startday As DayOfWeek = DayOfWeek.Friday
    Dim endday As DayOfWeek = DayOfWeek.Sunday
    Dim today As DayOfWeek = DateTime.Now.DayOfWeek

    If endday >= startday Then
        Return (today >= startday AndAlso today <= endday)
    Else
        Return (today >= startday OrElse today <= endday)
    End If
于 2013-05-27T13:48:42.997 回答
0

这似乎对我有用:

Sub Main()
  Debug.WriteLine(IsBetween(Now, DayOfWeek.Monday, DayOfWeek.Friday))
End Sub

Function IsBetween(vDate As DateTime, startDay As DayOfWeek, endDay As DayOfWeek)
  If startDay <= endDay Then Return vDate.DayOfWeek >= startDay And
                                    vDate.DayOfWeek <= endDay
  Return vDate.DayOfWeek <= endDay Or vDate.DayOfWeek >= startDay
End Function
于 2013-05-27T14:34:38.333 回答