Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple Zeller's congruence to get day of week
#2
(09-15-2024, 12:47 PM)TDarcos Wrote: I think this one is easier to understand than some of the others
Code: (Select All)

' Show-Date_and_Time.bas - Day of week computed using Zeller's congruence
' by Paul Robinson <paul@paul-robinson.us>
' September 15, 2024
' Dedicated to the Public Domain
Option _Explicit
Dim As String WeekDays(6), Months(12)
WeekDays(0) = "Saturday"
WeekDays(1) = "Sunday"
WeekDays(2) = "Monday"
WeekDays(3) = "Tuesday"
WeekDays(4) = "Wednesday"
WeekDays(5) = "Thursday"
WeekDays(6) = "Friday"
Months(1) = "January"
Months(2) = "February"
Months(3) = "March"
Months(4) = "April"
Months(5) = "May"
Months(6) = "June"
Months(7) = "July"
Months(8) = "August"
Months(9) = "September"
Months(10) = "October"
Months(11) = "November"
Months(12) = "December"
Dim As String CheckDate, CheckTime, AmPm
Dim As Integer Month, Day, Year, Hour, Minute, Second, WD
CheckDate = Date$
CheckTime = Time$
If CheckDate <> Date$ Then ' Midnight rolled over
    CheckDate = Date$
    CheckTime = Time$
End If
Month = Val(Left$(CheckDate, 2)): Day = Val(Mid$(CheckDate, 4, 2)): Year = Val(Right$(CheckDate, 4)):
Hour = Val(Left$(CheckDate, 2)): Minute = Val(Mid$(CheckDate, 4, 2)): Second = Val(Right$(CheckDate, 2))
WD = DayOfWeek(Month, Day, Year)
' Display date and time, formatted
Print "It is "; WeekDays(WD); " "; Months(Month); Str$(Day); ","; Year; " at";
$If MILITARYTIME Then
    print checktime
$Else
    AmPm = " AM"
    If Hour > 12 Then
        AmPm = " PM"
        Hour = Hour - 12
    End If
    Print Str$(Hour); ":"; Right$("0" + LTrim$(Str$(Minute)), 2); ":"; Right$("0" + LTrim$(Str$(Second)), 2); AmPm
$End If
End
' Returns 0=Saturday, etc.
Function DayOfWeek% (Month%, Day%, Year%)
    Dim As Integer I, J, K, D, M, Y
    D = Day%: M = Month%: Y = Year%
    If M < 3 Then
        M = M + 12
        Y = Y - 1
    End If
    K = Y Mod 100
    J = Y / 100
    DayOfWeek = (D + 13 * (M + 1) / 5 + K + K / 4 + J / 4 + 5 * J) Mod 7
End Function
A copy is attached to this message.
Some points
  1. Line 5 indicates I waive copyright on this file.
  2. Line 43 allows you to select military time format by having a line earlier in the program with "$LET MILITARY=-1". or drop the $IF block and keep the one you want (or make it a regular IF statement if the user gets to choose).
  3. The string functions around day and hour/minute/second are to make sure that day and hour don't have a trailing space, and so that minute and second have no leading or trailing spaces, but do have leading 0 if <10.
beautifully done 
But I found two errors.

1)
Code: (Select All)
Hour = Val(Left$(CheckDate, 2)): Minute = Val(Mid$(CheckDate, 4, 2)): Second = Val(Right$(CheckDate, 2))

That's probably what it should be called 

Code: (Select All)
Hour = Val(Left$(CheckTime, 2)): Minute = Val(Mid$(CheckTime, 4, 2)): Second = Val(Right$(CheckTime, 2))

2)
Line 43 allows you to select military time format by having a line earlier in the program with "$LET MILITARY=-1". or drop the $IF block

That's probably how it should be

Line 43 allows you to select military time format by having a line earlier in the program with "$LET MILITARYTIM=-1". or drop the $IF block
Reply


Messages In This Thread
RE: Simple Zeller's congruence to get day of week - by Steffan-68 - 09-15-2024, 02:21 PM



Users browsing this thread: 4 Guest(s)