2

I have a requirement that regardless of the start and dates that I need to loop through that timespan and calculate figures at the month level. I cannot seem to figure it out, and maybe it is not possible, but I would like to do something like:

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

Is this possible or do I need to calculate the number of months, and just iterate through the amount of months and calculate the start/end of that month based on my index?

4

2 に答える 2

1

タイムスパンは、日付のペアではなく、時間の長さです。ここには混乱があると思います。この場合、Timespan は特定の月 (2010-04-01 から 2010-04-31 など) ではなく、「1 か月」を表します。探していることを行うには、次のようなものが必要です (疑似コード):

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 に答える