Algorithm to calculate the number fortnightly occurring events in a given calendar month

StackOverflow https://stackoverflow.com/questions/4046698

  •  27-09-2019
  •  | 
  •  

Question

I'm looking for the cleverest algorithm for determining the number of fortnightly occurring events in a given calendar month, within a specific series.

i.e. Given the series is 'Every 2nd Thursday from 7 October 2010' the "events" are falling on (7 Oct 2010, 21 Oct, 4 Nov, 18 Nov, 2 Dec, 16 Dec, 30 Dec, ...)

So what I am after is a function

function(seriesDefinition, month) -> integer 

where:
    - seriesDefinition is some date that is a valid date in the series,
    - month indicates a month and a year

such that it accurately yeilds: numberFortnightlyEventsInSeriesThatFallInCalendarMonth

Examples:

NumberFortnightlyEventsInMonth('7 Oct 2010, 'Oct 2010') -> 2

NumberFortnightlyEventsInMonth('7 Oct 2010, 'Nov2010') -> 2

NumberFortnightlyEventsInMonth('7 Oct 2010, 'Dec 2010') -> 3

Note that October has 2 events, November has 2 events, but December has 3 events.


Psuedocode preferred.

I don't want to rely on lookup tables or web service calls or any other external resources other than potentially universal libraries. For example, I think we can safely assume that most programming languages will have some date manipulation functions available.

Was it helpful?

Solution 3

My solution ...

Public Function NumberFortnightlyEventsInMonth(seriesDefinition As Date, month As String) As Integer

    Dim monthBeginDate As Date
    monthBeginDate = DateValue("1 " + month)
    Dim lastDateOfMonth  As Date
    lastDateOfMonth = DateAdd("d", -1, DateAdd("m", 1, monthBeginDate))

    ' Step 1 - How many days between seriesDefinition and the 1st of [month]
    Dim daysToMonthBegin As Integer
    daysToMonthBegin = DateDiff("d", seriesDefinition, monthBeginDate)

    ' Step 2 - How many fortnights (14 days) fit into the number from Step 1?  Round up to the nearest whole number.
    Dim numberFortnightsToFirstOccurenceOfSeriesInMonth As Integer
    numberFortnightsToFirstOccurenceOfSeriesInMonth = (daysToMonthBegin \ 14) + IIf(daysToMonthBegin Mod 14 > 0, 1, 0)

    ' Step 3 - The date of the first date of this series inside that month is seriesDefinition + the number of fortnights from Step 2
    Dim firstDateOfSeriesInMonth As Date
    firstDateOfSeriesInMonth = DateAdd("d", (14 * numberFortnightsToFirstOccurenceOfSeriesInMonth), seriesDefinition)

    ' Step 4 - How many fortnights fit between the date from Step 3 and the last date of the [month]?
    NumberFortnightlyEventsInMonth = 1 + (DateDiff("d", firstDateOfSeriesInMonth, lastDateOfMonth) \ 14)

End Function

OTHER TIPS

There is no "clever" algorithm when handling dates, there is only the tedious one. That is, you have to specifically list how many days are in each month, handle leap years (every four years, except every 100 years, except every 400 years), etc.

Well, for the algorithm you are talking about the usual solution is to calculate the day number starting from some fixed date. (Number of day plus cumulated number of days in prev months plus number of years * 365 minus (number of year / 4) plus (number of year / 100) minus (number of year / 400))

Having this, you can easily implement what you need to. You need to calculate which day of week was the 1 January 1. Then you can easily see what is the number of "every second thursdays" from that day to 1 Oct 2010 and 1 Dec 2010. their difference is the value you are looking for.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top