PDA

View Full Version : Is ElectionDay Function



gmaxey
10-09-2018, 10:02 AM
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.

macropod
10-09-2018, 02:09 PM
Presumably, that'd be something like:

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

gmaxey
10-09-2018, 02:43 PM
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:



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


Thank for looking at it though.

macropod
10-09-2018, 02:51 PM
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.

gmaxey
10-09-2018, 03:06 PM
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.

macropod
10-09-2018, 03:12 PM
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:

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
Change Mod 2 to Mod 4 if you're only interested in the Presidential elections.

gmaxey
10-09-2018, 03:49 PM
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.