views:

47

answers:

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?

+1  A: 

A Timespan is a length of time, not a pair of Dates - I think there's confusion here. The Timespan represents "1 month" in this case, not a particular month (like 2010-04-01 to 2010-04-31). To do what you're looking for, you'd need something like this (Psuedo-code):

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
rwmnau
That is kinda what I figured I needed to do, was hoping there was a slicker way to do it.
IPX Ares
A: 

I would do something like this:

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
Jhonny D. Cano -Leftware-