VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Mathematically calculate the day of the week.

by Garnet Tozer (1 Submission)
Category: Math/Dates
Compatability: Visual Basic 3.0
Difficulty: Unknown Difficulty
Originally Published: Wed 22nd November 2000
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Mathematically calculate the day of the week.

Rate Mathematically calculate the day of the week.



    Dim DOW As Integer
    DOW = DayOfWeek(2000, 11, 22)   ' November 22, 2000
    Select Case DOW
        Case 0:
            Print "Sunday"
        Case 1:
            Print "Monday"
        Case 2:
            Print "Tuesday"
        Case 3:
            Print "Wednesday"
        Case 4:
            Print "Thursday"
        Case 5:
            Print "Friday"
        Case 6
            Print "Saturday"
        Case Else
            Print "Error"
    End Select
End Sub

Private Function DayOfWeek(y As Integer, m As Integer, d As Integer)
    ' Programmatically calculate the day of the week from a given date.
    
    ' DOW = ([23m/9] + d + 4 + z + [z/4] - [z/100] + [z/400] - 2 (if m>=3)) mod 7
    '
    ' y is the 4 digit year
    ' m is the month
    ' d is the day.
    ' z = y - 1 (if m <  3)
    '   = y     (if m >= 3)
    ' A mod B means take the reminder of A / B.
    '
    ' Sunday = 0, Monday = 1, etc.
    '
    If m < 3 Then
       y = y - 1
    Else
       y = y - 2
    End If
    
    DayOfWeek = ((23 * m / 9 + d + 4 + y + (y / 4) - (y / 100) + (y / 400)) Mod 7)
End Function



Download this snippet    Add to My Saved Code

Mathematically calculate the day of the week. Comments

No comments have been posted about Mathematically calculate the day of the week.. Why not be the first to post a comment about Mathematically calculate the day of the week..

Post your comment

Subject:
Message:
0/1000 characters