我需要在时间范围内找到 a)总周数,还要 b)每周的开始/结束日期(周一,周日)(fromDate ,toDate)以下列方式:
fromDate: Thursday, 9/1/2016
toDate: Friday, 10/14/2016
----------------------------------------------------
Week1: Thursday, 9/1/2016 - Sunday, 9/4/2016
Week2: Monday, 9/5/2016 - Sunday, 9/11/2016
Week3: Monday, 9/12/2016 - Sunday, 9/18/2016
Week4: Monday, 9/19/2016 - Sunday, 9/25/2016
Week5: Monday, 9/26/2016 - Sunday, 10/2/2016
Week6: Monday, 10/3/2016 - Sunday, 10/9/2016
Week7: Monday, 10/10/2016 - Friday, 10/14/2016
所以我还需要以正式的日历方式填充在乞讨时和结束时的不完整周(从日期到日期)
我已经在VB中实现了一个逻辑但是我错过了最后一周,在某些情况下,toDate日是:星期一和星期二。因此,某些参数或确切逻辑不是100%正确的。代码如下:
Public Shared Function GetWeeksFromTimespan(ByVal fromDate As Date, ByVal toDate As Date) As List(Of WeeksFromTimespan)
Dim ListOfWeekTimespans As List(Of WeeksFromTimespan) = New List(Of WeeksFromTimespan)
' WeeksFromTimespan structure: WeekNumber As String, WeekNumber As Integer, fromDateWeek As Date, toDateWeek As Date
' fromDateWeek: week start date
' toDateWeek: week end date
Dim Span As TimeSpan = toDate.Subtract(fromDate)
Dim WeekCount As Integer = 0
Dim Days As Integer = 0
Dim DayCount As Integer = 0
If Span.Days <= 7 Then
If fromDate.DayOfWeek > toDate.DayOfWeek Then
WeekCount = 2
Dim DaysUntilEndOfFWeek As Integer = 0
DaysUntilEndOfFWeek = 7 - fromDate.DayOfWeek
Dim firstWeekEndDate As Date = fromDate.AddDays(DaysUntilEndOfFWeek)
ListOfWeekTimespans.Add(New WeeksFromTimespan("Week1",1,fromDate,firstWeekEndDate))
Dim secondWeekStartDate As Date = firstWeekEndDate.AddDays(1)
ListOfWeekTimespans.Add(New WeeksFromTimespan("Week2",2,secondWeekStartDate,toDate))
Else
WeekCount = 1
ListOfWeekTimespans.Add(New WeeksFromTimespan("Week1",1,fromDate,toDate))
End If
Else
Days = Span.Days - 7 + CInt(fromDate.DayOfWeek)
WeekCount = 1
DayCount = 0
Dim DaysUntilEndOfFWeek As Integer = 0
DaysUntilEndOfFWeek = 7 - fromDate.DayOfWeek
While DayCount <= Days
If(DaysUntilEndOfFWeek>0) Then
ListOfWeekTimespans.Add(New WeeksFromTimespan("Week" + WeekCount.ToString,WeekCount,fromDate.AddDays(DayCount),fromDate.AddDays(DaysUntilEndOfFWeek)))
DayCount += DaysUntilEndOfFWeek+1
WeekCount += 1
DaysUntilEndOfFWeek = 0
Else
If(fromDate.AddDays(DayCount+6)<=toDate) Then
ListOfWeekTimespans.Add(New WeeksFromTimespan("Week" + WeekCount.ToString,WeekCount,fromDate.AddDays(DayCount),fromDate.AddDays(DayCount+6)))
Else
ListOfWeekTimespans.Add(New WeeksFromTimespan("Week" + WeekCount.ToString,WeekCount,fromDate.AddDays(DayCount),toDate))
End If
DayCount += 7
WeekCount += 1
End If
End While
End If
return ListOfWeekTimespans
End Function
但是,当上一个日期(toDate)在星期一,星期二时,上面的代码不会填充最后一个INCOMPLETE WEEK。例如:
fromDate: 1/9/2016 (Thursday)
toDate: 11/10/2015 (Tuesday)
-----------------------------------------------
Week1: Thursday, 9/1/2016 - Sunday, 9/4/2016
Week2: Monday, 9/5/2016 - Sunday, 9/11/2016
Week3: Monday, 9/12/2016 - Sunday, 9/18/2016
Week4: Monday, 9/19/2016 - Sunday, 9/25/2016
Week5: Monday, 9/26/2016 - Sunday, 10/2/2016
Week6: Monday, 10/3/2016 - Sunday, 10/9/2016
所以使用上面的代码,我错过了最后一个不完整的一周:
Week7: Monday, 10/10/2016 - Tuesday, 11/10/2015.
需要优化此实施,以便能够准确计算两个日期之间“明确”周的考试编号和日期。