;; 
;;=====================================================================================----- 
;; 
;;FUNCTION       WeekDay() 
;; 
;;ACTION         Returns the numeric weekday value or day name for a given date 
;; 
;;AUTHOR         Glenn Barnas 
;; 
;;VERSION        1.0  - 2013/12/20 
;; 
;;HISTORY        1.0  - 2013/12/20 - Original Release 
;;               1.1  - 2019/03/08 - Added flag to return Day Name 
;;                
;; 
;;SYNTAX         WeekDay(date[, fDName) 
;; 
;;PARAMETERS     date   - REQUIRED - Returns the weekday for a given date - 1=Mon, 7=Sun 
;; 
;;               fDName - OPTIONAL - If true, returns the day name 
;; 
;;REMARKS         
;; 
;;RETURNS         
;; 
;;DEPENDENCIES   none 
;; 
;;TESTED WITH    W2K8, W2K12, W2K16 
;; 
;;EXAMPLES        
; 
Function WeekDay($_Date, Optional $_fDName)
 
  Dim $_Y, $_M, $_D					; Year, Month, and Day values 
 
  $_Date=split($_Date,'/')				; split and set individual values 
  $_Y = Val($_Date[0])
  $_M = Val($_Date[1])
 
  If $_M > 12 Exit 1901 EndIf				; not valid 
 
  $_D = Val($_Date[2])					; process the Day 
  If $_D > 31 Exit 1901 EndIf
  If $_Y <= 1582 and $_M <= 10 and $_D <= 15 Exit 1901 EndIf
  If $_M < 3
    $_M = $_M + 12
    $_Y = $_Y - 1
  EndIf
  ; adjust for leap years 
  $_Date = $_D + (153 * $_M - 457) / 5 + 365 * $_Y + $_Y / 4 - $_Y / 100 + $_Y / 400 - 306
 
  $WeekDay = ($_Date - 577738 + 6) mod 7 + 1
 
  If $_fDName             ; 1234567890123456789012345678901234567890123456789012345678901234567890123456789 
    $WeekDay = Trim(SubStr('          Monday    Tuesday   Wednesday Thursday  Friday    Saturday  Sunday    ',10 * $WeekDay, 10))
  EndIf
 
  Exit 0
 
EndFunction