类似与outlook的一个约会预订的其中按周来重复预订周期
功能是如下;
页面的元素为:
1。开始时间: startDate
2。按每1周(这里不是固定的,可以是N周,用户自己填写)
3。下面供选择星期几分别是
周日,周一,周二,周三,周四,周五,周六
4。出现N次结束 ,N也是用户自己填写的,
比如用户的一个实际的例子
开始时间: 2008-8-1
按照每1周 ,周一,周三,周四 循环出现10次后结束
请问要怎么去判断,我的想法是取得最后一次的时间就出来了,
------解决方案--------------------------------------------------------
根据你的需求,代码如下:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim recurrWeeklyWeeks As String = "2 "
Dim recurrEndOccurence As Integer = 10
Dim recurrWeeklyOnWeekDay As String = "1,3,4 "
Dim recurrStartDate As DateTime = "2007-8-1 "
GetFinallyRecurrEndOccurenceDate(recurrWeeklyWeeks, recurrEndOccurence, recurrWeeklyOnWeekDay, recurrStartDate)
End Sub
Public Sub GetFinallyRecurrEndOccurenceDate(ByVal recurrWeeklyWeeks As String, ByVal recurrEndOccurence As Integer, _
ByVal recurrWeeklyOnWeekDay As String, _
ByVal recurrStartDate As DateTime)
Dim recurrWeekDay As Integer = recurrStartDate.DayOfWeek() ' current datetime is which day
Dim weeklyOnWeekDay As String() = recurrWeeklyOnWeekDay.Split( ", ")
Dim first As String = String.Empty ' Get min WeekDay
Dim last As String = String.Empty 'Get last WeekDay
Dim endDate As DateTime
' if weekly Array have 1 weekday ,return end date
' otherwise that must continue to check
If weeklyOnWeekDay.Length = 1 Then
endDate = recurrStartDate.AddDays(CType(recurrWeeklyWeeks, Integer) * 7)
Else
If weeklyOnWeekDay.Length = 1 Then
first = weeklyOnWeekDay(0)
last = weeklyOnWeekDay(0)
Else
first = weeklyOnWeekDay(0)
last = weeklyOnWeekDay(weeklyOnWeekDay.Length - 1)
End If
Dim maxWeekDay As DateTime = recurrStartDate.AddDays(CType(last, Integer) - CType(first, Integer)).AddDays(CType(recurrWeeklyWeeks, Integer) * 7 * recurrEndOccurence) '计算出最大一个星期的是的时间
Dim minWeekDay As DateTime = recurrStartDate
Dim numOfRecurr As Integer = 0
For i As Integer = 0 To maxWeekDay.Subtract(minWeekDay).Days
endDate = recurrStartDate.AddDays(i)
If Math.Round(endDate.Subtract(recurrStartDate).Days / 7) Mod (CType(recurrWeeklyWeeks, Integer)) = 0 Then
For j As Integer = 0 To weeklyOnWeekDay.Length - 1
If CType(endDate.DayOfWeek(), Integer) = CType(weeklyOnWeekDay(j), Integer) Then
numOfRecurr += 1
If recurrEndOccurence = numOfRecurr Then
Response.Write( "End at : " + endDate.ToString() + " <br> ")
Exit Sub
Else
Response.Write(endDate.ToString() + " <br> ")
End If
End If
Next
End If
Next
End If
End Sub