Can I make Foreach on TimeSpan like Timespan?

I have a requirement that regardless of the start and the dates I need to go through this time span and calculate the numbers at the month level. I cannot figure it out and it might not be 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 loop over the number of months and calculate the start / end of that month based on my index?

+2


a source to share


2 answers


The time slot is a time, not a couple of dates - I think this is confusing. In this case, Timespan represents "1 month" and not a specific month (for example, 2010-04-01 - 2010-04-31). To do what you are looking for, you 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

      

+1


a source


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

      

0


a source







All Articles