09-15-2024, 12:47 PM
(This post was last modified: 09-17-2024, 01:16 AM by TDarcos.
Edit Reason: add advisory
)
*** SPECIAL NOTICE *** I am editing this post on 9/16 to warn people reading this later that the Zeller's congruence formula I use below has an error. Whether that is because the formula was posted incorrectly or I copied it wrong, some dates it gets wrong, e.g. 12/31/2029, the program returns Tuesday, when the correct date is Monday. In any case, I accept sole responsibility for this error. Which means I did not change anything below, as owning one's mistakes means you don't hide them either.
I think this one is easier to understand than some of the others
Some points
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
- Line 5 indicates I waive copyright on this file.
- 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).
- 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.
While 1
Fix Bugs
report all bugs fixed
receive bug report
end while
Fix Bugs
report all bugs fixed
receive bug report
end while