Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1449

[VB6/VBA] "modern" Version of IsLeapYear and LastDayOfMonth-Functions

$
0
0
Hi Folks,
was watching this fascinating video this weekend:
"Implementing Fast Calendar Algorithms"
https://www.youtube.com/watch?v=J9KijLyP-yg

And i caught myself thinking: Huh? I've been doing the "classical" way all that time

So i searched through the forums here, and pretty much every result showed the "classical" way

So i sat down and rewrote their C/C++-Code in VB6/VBA

Code:

Public Function IsLeapYear(ByVal AYear As Long) As Boolean
    If AYear Mod 100 <> 0 Then
        IsLeapYear = (AYear Mod 4) = 0
    Else
        IsLeapYear = (AYear Mod 16) = 0
    End If
End Function

'Could also use ... As Integer
Public Function LastDayOfMonth(ByVal AYear As Long, ByVal AMonth As Long) As Long
    If AMonth = 2 Then
        If IsLeapYear(AYear) Then
            LastDayOfMonth = 29
        Else
            LastDayOfMonth = 28
        End If
    Else
        LastDayOfMonth = 30 Or (9 * AMonth \ 8)
    End If
End Function

Yes, i'm aware, that there is a "Version" for LastDayOfMonth with a lookup-Array, but in VB6/VBA we would have to initialize that Array first (since constant arrays are not supported),
so i refrained from showing it here

Wouldn't mind reviews or Performance-Test-Results
Comments welcome

Viewing all articles
Browse latest Browse all 1449

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>