Morrg
Morrg

Reputation: 5

Calculate If Date Is Every Other Friday And If So Do Something

So I need to know how I can grab every other Friday starting with the date of a Friday, have the program check if the date is one of those dates and if so do X function. So far I found how to find the Friday after one Friday, but I need it to gather up the dates and save them then check against them.

Dim payday as Date= GetNext(DayOfWeek.Friday) 

Function GetNext(ByVal d As DayOfWeek, Optional ByVal StartDate As Date = Nothing) As Date
    If StartDate = DateTime.MinValue Then StartDate = Now
    For p As Integer = 1 To 7
        If StartDate.AddDays(p).DayOfWeek = d Then Return StartDate.AddDays(p)
    Next
End Function

Then Here is the If Statement I am thinking of

If Date.Today = payday Then
    'do this
End If

Rough sketch of what I was thinking.

Upvotes: 0

Views: 147

Answers (1)

Zev Spitz
Zev Spitz

Reputation: 15375

Dim startDate As Date 'fill with an initial Friday
Function IsPayday(Optional dte as Date? = Nothing) As Boolean
    dte = If(dte, Today)
    Return DateDiff(DateInterval.Day, startDate, dte) Mod 14 = 0
End Function

and use it:

Dim yesterday = Today.AddDays(-1)
If IsPayday(yesterday) Then
    'yesterday was a pay day
End If
If IsPayday() Then
    'today is a pay day
End If

Upvotes: 3

Related Questions