Dim NextFriday as Date= GetNext(DayOfWeek.Friday)
Function GetNext(ByVal d As DayOfWeek, Optional ByVal StartDate As Date = Nothing) As Date
If StartDate = DateTime.MinValue Then StartDate = Now
For p As Integer = 1 To 7
If StartDate.AddDays(p).DayOfWeek = d Then Return StartDate.AddDays(p)
Next
End Function
Run Code Online (Sandbox Code Playgroud)
编辑:更新答案以允许startdate(可选).
下周五:
Dim NextFriday As Date = GetNext(DayOfWeek.Friday)
Run Code Online (Sandbox Code Playgroud)
从现在起15天后的下周五是什么时候:
Dim AnotherFriday As Date = GetNext(DayOfWeek.Friday,now.addays(15))
Run Code Online (Sandbox Code Playgroud)
'
Public Function nextDOW(whDayOfWeek As DayOfWeek, _
Optional theDate As DateTime = Nothing) As DateTime
'returns the next day of the week
If theDate = Nothing Then theDate = DateTime.Now
Dim d As DateTime = theDate.AddDays(whDayOfWeek - theDate.DayOfWeek)
Return If(d <= theDate, d.AddDays(7), d)
End Function
Run Code Online (Sandbox Code Playgroud)
Dim someDate As DateTime = ... 'input date
Dim nextFriday As DateTime = someDate
While nextFriday.DayOfWeek <> DayOfWeek.Friday
nextFriday = nextFriday.AddDays(1)
End While
Console.WriteLine(nextFriday)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8437 次 |
| 最近记录: |