Script to find the day of the week

From MikroTik Wiki
Revision as of 10:49, 23 July 2009 by Normis (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This script will find the weekday (sun,mon,tue, etc.) for a given date.

Tested on 3.25, 3.26, and 3.27

# Calculates day of the week for a givien date
# Month: jan,feb ... nov,dec   (must be lower-case)
# Day: 1 - 31
# Year: 1900 - 2999
# mmm/dd/yyyy   same format as [/system clock get date]
# (ex. jul/22/2009)

:local date [/system clock get date]



# Math Calculation here
:local result ""
:local months [:toarray "jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec"]
:local monthtbl [:toarray "0,3,3,6,1,4,6,2,5,0,3,5"]
:local daytbl [:toarray "sun,mon,tue,wed,thu,fri,sat"]

:local month [:pick $date 0 3]
:local day [:pick $date 4 6]
:local century [:pick $date 7 9]
:local year [:pick $date 9 11]

:local sum 0
:set sum ($sum + (2 * (3 - ($century - (($century / 4) * 4)))))
:set sum ($sum + ($year / 4))
:set sum ($sum + $year + $day)
:for mindex from=0 to=[:len $months] do={
  :if ([:pick $months $mindex] = $month) do={:set sum ($sum + [:pick $monthtbl $mindex]) }
}
:set sum ($sum - (($sum / 7) * 7))
:set result [:pick $daytbl $sum]

# END Math Calculation

:put ($month . "/" . $day . "/" . $century . $year . " is on a " . $result)