2

我有一个要求,无论开始和日期如何,我都需要遍历该时间跨度并计算月份级别的数字。我似乎无法弄清楚,也许这是不可能的,但我想做类似的事情:

FOREACH Month As TimeSpan in ContractRange.Months
   Do Calculations (Month.Start, Month.End)
NEXT

这是可能的还是我需要计算月数,然后遍历月数并根据我的索引计算该月的开始/结束?

4

2 回答 2

1

Timespan 是一段时间,而不是一对 Dates - 我认为这里存在混淆。在这种情况下,时间跨度表示“1 个月”,而不是特定月份(如 2010-04-01 到 2010-04-31)。要做你正在寻找的东西,你需要这样的东西(伪代码):

Get the number of months between start and end of contract range
For each month in that list
    determine start and end of that month
    Do your calculations(start, end)
next month
于 2010-04-26T16:54:16.770 回答
0

我会做这样的事情:

Dim CurrentDate, StartDate, EndDate as DateTime

' Get dates for contract range
GetDatesForContractRange(StartDate, EndDate) ' ref? i'm not a Vb guy

CurrentDate = StartDate
While CurrentDate < EndDate

    ' Calculate month bounds
    Dim StartMonth, EndMonth as DateTime
    StartOfMonth = CurrentDate.AddDays(-CurrentDate.Day + 1)
    EndOfMonth = StartOfMonth.AddMonths(1).AddDays(-1) ' or AddSeconds(-1)

    Do Calculations (StartOfMonth, EndOfMonth)
    CurrentDate = CurrentDate.AddMonths(1)
End While
于 2010-04-26T16:59:35.903 回答