Day of Week on Given Date (Method #1)

Category: Time/Timers

Date: 02-16-2022

Return to Index


 
'Knowing which day of the week a date falls on can be very useful.
'The equations for the Astronomical Day (also called the Julian Date)
'of a date can be used to make the calculation.
 
'Primary Code:
'Credit: MSDN  http://support.microsoft.com/kb/109451
Function AstroDay(year As Long, month As Long, day As LongAs Long
   Dim y As Double
   y = year + (month - 2.85) / 12
   AstroDay = Int(Int(Int(367 * y) - 1.75 * Int(y) + day) -0.75 * Int(0.01 * y)) + 1721119
End Function
 
 
'Compilable Example:  (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg As Dword
 
Function PBMain() As Long
   Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
   '   Control Add Label, hDlg, 130,"9-12-1951", 50,20,100,20
   Control Add Label, hDlg, 130,"9-12-2009", 50,20,100,20
   Control Add Button, hDlg, 140,"Push", 50,50,100,20
   Dialog Show Modal hDlg Call DlgProc
End Function
 
CallBack Function DlgProc() As Long
   If CB.Msg = %WM_Command AND CB.Ctl = 140 AND CB.Ctlmsg = %BN_Clicked Then
      Local Day As Long, y As Long, m As Long, d As Long, temp$
      Control Get Text hDlg, 130 To temp$
      y = Val(Parse$(temp$, "-", 3))
      m = Val(Parse$(temp$, "-", 1))
      d = Val(Parse$(temp$, "-", 2))
      Day = (AstroDay(y, m, d)-3) Mod 7  ' Day of week 0=Sunday, 6=Saturday
      MsgBox Choose$(Day+1, "Sun", "Mon","Tue","Wed","Thur","Fri","Sat"), %MB_Ok, "Day of Week"
   End If
End Function
 
Function AstroDay(year As Long, month As Long, day As LongAs Long
   Dim y As Double
   y = year + (month - 2.85) / 12
   Function = Int(Int(Int(367 * y) - 1.75 * Int(y) + day) -0.75 * Int(0.01 * y)) + 1721119
End Function
 
'gbs_00284
'Date: 03-10-2012


created by gbSnippets
http://www.garybeene.com/sw/gbsnippets.htm