Does anyone have a function to determine if a given date falls on Election Day of the given year?
For example, if I passed 11/5/2018 to the function it would return False. If I passed 1/6/2018 it would return true.
Thanks.
Printable View
Does anyone have a function to determine if a given date falls on Election Day of the given year?
For example, if I passed 11/5/2018 to the function it would return False. If I passed 1/6/2018 it would return true.
Thanks.
Presumably, that'd be something like:
Code:Function IsElectionDay(Dt As Date) As Boolean
If (Format(Dt, "m/d") = "1/6") And (Format(Dt, "yyyy") Mod 4 = 2) Then
IsElectionDay = True
Else
IsElectionDay = False
End If
End Function
Paul,
My understanding is that the U.S. Election Day is the first Tuesday (after the first Monday) in November. Clunky perhaps, but the following seems to work:
Thank for looking at it though.Code:Sub TE()
MsgBox fcnIsElectionDay("11/6/2018")
MsgBox fcnIsElectionDay("11/6/2019") 'False
MsgBox fcnIsElectionDay("11/5/2019")
MsgBox fcnIsElectionDay("11/8/2022")
End Sub
Public Function fcnIsElectionDay(oDate As Date) As Boolean
Dim lngMonth As Integer, lngWD As Integer
fcnIsElectionDay = False
lngWD = Weekday(oDate)
lngMonth = Month(oDate)
If lngMonth = 11 And lngWD = 3 Then
If Month(DateAdd("d", -1, oDate)) = 11 Then
If Day(oDate) < 9 And Day(oDate) > 1 Then
fcnIsElectionDay = True
End If
End If
End If
lbl_Exit:
Exit Function
End Function
So how, in your first post, would 1/6/2018 return true? FWIW, not knowing the US political system in detail, I assumed that date was the post-election inauguration day and that's what you meant.
Paul,
Don't you just love it when people can't pay enough attention to get the details right? That was typo. I meant 11/6/2018. Yes, I've kicked myself in my own head for it.
Don't you need to test the year, too? AFAIK, the presidential election is only every 4th year and there are mid-term elections 2 years after that but none is held in the 1st or 3rd years. Correct? If so, try:
Change Mod 2 to Mod 4 if you're only interested in the Presidential elections.Code:Function IsElectionDay(Dt As Date) As Boolean
IsElectionDay = False
If Format(Dt, "yyyy") Mod 2 = 0 Then
If Format(Dt, "mm") = 11 Then
If Dt Mod 7 = 3 Then
If (Format(Dt, "d") > 1) And (Format(Dt, "d") < 9) Then
IsElectionDay = True
End If
End If
End If
End If
End Function
Paul,
Yes, for Federal Elections that is true. For my purposes though, I was using the Pennsylvania statute which declares the First Tuesday (after the first Monday) in November (regardless of year) as Election day. I suppose they must have state or local elections every year.
Thanks. Yes, your function works for what you defined and works well.