如何知道日期10和25是星期六还是星期日

时间:2014-05-06 01:48:05

标签: vb.net

如何判断vb.net中特定月份dates 10 and 25是否在Saturday or Sunday上?

If dates is on Saturday then
  msgbox(cut off move to friday)
elseif dates is on Sunday then
  msgbox(cut off move to monday)

2 个答案:

答案 0 :(得分:2)

只需检查DateTime结构的DayOfWeek属性。

If dates.DayOfWeek = DayOfWeek.Saturday then
  msgbox(cut off move to friday)
elseif dates.DayOfWeek = DayOfWeek.Sunday then
  msgbox(cut off move to monday)

答案 1 :(得分:1)

正如Brendan Green指出的那样,只需比较日期时间的DayOfWeek财产:

Sub Main
    Dim dateToCompare As New DateTime(2014, 5, 10)

    Dim cutoff As DateTime

    If dateToCompare .DayOfWeek = DayOfWeek.Saturday Then    
        cutoff = dateToCompare .AddDays(-1)
    Else
        If dateToCompare .DayOfWeek = DayOfWeek.Sunday  Then
            cutoff = dateToCompare .AddDays(1)
        Else
            cutoff = dateToCompare
        End If
    End If

    Console.WriteLine("The cutoff date is {0}", cutoff)
End Sub