Community discussions

 
spotts78
newbie
Topic Author
Posts: 31
Joined: Thu Dec 03, 2009 5:59 pm
Location: Greensboro, NC - USA

Calculate Day of Week from Date

Fri Feb 03, 2012 5:18 am

I've tried the script here http://wiki.mikrotik.com/wiki/Script_to ... f_the_week for calculating the day of the week from a given date and it doesn't seem to give me the correct answer. Is it broken, am I doing something wrong?

So in an effort to improve my scripting skills and using that as a base I've come up with this based on my understanding of Zeller's algorithm...
It's gets a little messy without decimal math in the scripting language, but seems to work and give the "right" answers for dates after 1752. Feedback please, I'm sure there are flaws and it can be improved upon?
#use the system clock date or one of our choosing
#:local date [/system clock get date]
:local date jan/12/2019

#Variables
:local result ""
:local daytbl [:toarray "sun,mon,tue,wed,thu,fri,sat"]
:local MonthNum 0
:local CenturyNum 0
:local YearNum 0
:local SUM 0


#Parse the input date
:local month [:pick $date 0 3]
:local day [:pick $date 4 6]
:local year [:pick $date 7 11]

#perform the month offset per the algorithm
:if ($month = "jan") do={ :set MonthNum 11}
:if ($month = "feb") do={ :set MonthNum 12}
:if ($month = "mar") do={ :set MonthNum 1}
:if ($month = "apr") do={ :set MonthNum 2}
:if ($month = "may") do={ :set MonthNum 3}
:if ($month = "jun") do={ :set MonthNum 4}
:if ($month = "jul") do={ :set MonthNum 5}
:if ($month = "aug") do={ :set MonthNum 6}
:if ($month = "sep") do={ :set MonthNum 7}
:if ($month = "oct") do={ :set MonthNum 8}
:if ($month = "nov") do={ :set MonthNum 9}
:if ($month = "dec") do={ :set MonthNum 10}

#perform the year offset based on month per the algorithm
:if ($MonthNum = 11) do={ :set year ($year-1)}
:if ($MonthNum = 12) do={ :set year ($year-1)}

#start the date math
:set CenturyNum ($year/100)
:set YearNum ($year % 100)

#gets a little weird here without decimals...
:local decimalOne "2.6"
:local decimalTwo ".2"

#do some decimal math
:set SUM ($decimalOne * $MonthNum - $decimalTwo)

#looks like numbers get converted to date data type, strip off what we need
:set SUM [:pick $SUM 6 8]

:set SUM ($SUM + $day + $YearNum)

:set SUM ($SUM + $YearNum/4)

:set SUM ($SUM + $CenturyNum/4)

:set SUM ($SUM - 2 * $CenturyNum)

:set SUM ($SUM % 7)

:if ($SUM < 0) do= {:set SUM ($SUM+7)}

:set result [:pick $daytbl $SUM]
:put $result
 
User avatar
iNetSpec
just joined
Posts: 4
Joined: Wed Feb 08, 2012 12:30 am

Re: Calculate Day of Week from Date

Thu Feb 09, 2012 8:58 pm

I encountered a similar experience last year when I needed to write a scheduled script to enable and disable a wireless interface on specific days of the week. I ended up with the following "function" that expands the information and allows you to put the pieces back together in whatever format you desire.

I use this whenever I need more information than "/ system clock get date" provides.

Please feel free to use this if you like it.
#
#	$RCSfile: function-date.rsc,v $
#	$Revision: 1.1 $
#	$Date: 2011/05/27 19:47:46 $
#	$Author: reesejb $, iNet Specialists. Copyright, (C)2007-2011, All Rights Reserved
#
#	Calculates day of the week (number of month, etc.) for a givien date
#
#	Expanded from "day of week" script at:
#		http://wiki.mikrotik.com/wiki/Script_to_find_the_day_of_the_week
#	
#	Month: jan,feb,..nov,dec   (must be lower-case)
#	Day: 01-31
#	Year: 1900-2999
#	mmm/dd/yyyy   same format as [/system clock get date]
#	(ex. jul/22/2009)

:local date [/ system clock get date]

# Variables
:global weekday ""
:global monthord ""
:local months [:toarray "jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec"]
:local monthval [:toarray "01,02,03,04,05,06,07,08,09,10,11,12"]
: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"]

:global monthshort [:pick $date 0 3]
:global day [:pick $date 4 6]
:local daysum $day
:local dayc [:pick $date 5 6]
:global century [:pick $date 7 9]
:global year [:pick $date 9 11]
:local yearsum $year
:local yearc [:pick $date 10 11]

# if the first digit of the day or year is a 0 (zero),
# only use the last digit, otherwise the script will fail
:if ([:pick $date 4 5] = 0) do={ :set daysum ($dayc)}
:if ([:pick $date 9 10] = 0) do=[:set yearsum ($yearc)]

# Calculate "sum" to determine which "day of week" (from daytbl) to use
:local sum 0
:set sum ($sum + (2 * (3 - ($century - (($century / 4) * 4)))))
:set sum ($sum + ($yearsum / 4))
:set sum ($sum + $yearsum + $daysum)
:for mindex from=0 to=[:len $months] do={ \
	:if ([:pick $months $mindex] = $monthshort) do={ \
		:set sum ($sum + [:pick $monthtbl $mindex])
		:set monthord [:pick $monthval $mindex]
		} \
	}
:set sum ($sum - (($sum / 7) * 7))
:set weekday [:pick $daytbl $sum]

# Log the result for troubleshooting purposes
:log info "Today is $weekday, $monthshort $day, $century$year ($monthord/$day/$century$year)"
 
spotts78
newbie
Topic Author
Posts: 31
Joined: Thu Dec 03, 2009 5:59 pm
Location: Greensboro, NC - USA

Re: Calculate Day of Week from Date

Fri Feb 10, 2012 1:17 am

I ran your script and it gave me an incorrect result for today. It's Thursday February 9, 2012 and your function says it's Friday???
 
melboyscout
just joined
Posts: 7
Joined: Wed Nov 20, 2013 9:15 pm

Re: Calculate Day of Week from Date

Sat Nov 23, 2013 12:32 pm

Test my script, please
# Calculates day of the week for a givien date
# Month: jan,feb ... nov,dec   (must be lower-case)
# Day: 1 - 31
# Year: 1583 - ...
# mmm/dd/yyyy   same format as [/system clock get date]
# (ex. feb/19/2012)
# by melboyscout (melboyscout [at] gmail.com)

: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 daytbl [:toarray "sun,mon,tue,wed,thu,fri,sat"]

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

# if the first char is a 0 (zero) only read last char, else script fails
:if ([:pick $date 4 5] = 0) do={ :set day ($dayc)}

:local sum 0
:local aaa 0
:local yyy 0
:local mmm 0
:local nmonth 1

:for mindex from=0 to=[:len $months] do={
  :if ([:pick $months $mindex] = $month) do={:set nmonth ($mindex + 1) }
}

:set aaa ((14 - $nmonth) / 12)
:set yyy ($year - $aaa)
:set mmm ($nmonth + 12 * $aaa - 2)
:set sum (7000 + $day + $yyy + ($yyy / 4) - ($yyy / 100) + ($yyy / 400) + ((31 * $mmm) / 12))
:set sum ($sum - (($sum / 7) * 7))
:set result [:pick $daytbl $sum]
:log info "Today is $result"
 
tedkuban
just joined
Posts: 9
Joined: Wed Apr 16, 2014 9:15 am

Re: Calculate Day of Week from Date

Fri Oct 20, 2017 2:10 pm

Test my script, please
Worked for me.

Can you create one more algorithm which calculates number of week in a year?
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 1294
Joined: Sat Dec 24, 2016 11:17 am
Location: jo.overland at gmail.com

Re: Calculate Day of Week from Date

Sat Oct 21, 2017 10:21 am

If someone is better than me in programming, you can find the formula to find week number here under IS8601 section:

http://www.proesite.com/timex/wkcalc.htm
 
How to use Splunk to monitor your MikroTik Router

MikroTik->Splunk
 

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 69 guests