Date Parts --> Weekday?

Good Morning Mikrotik Community,
Does anyone know if there’s a way to get weekday (either as a number or a word, I don’t really care at this stage) from the current date in a MikroTik script.
In my current automatic backup script, I have:

:local theDate [/system clock get date]
:local theDay [ :pick \$theDate 4 6 ]

I can see that the :pick just extracts parts of the date string.

At the moment, my automatic backup script just creates a new config export every day and then I have a small .NET program running on server on our network which logs in with SSH and downloads it.
What I would like to have is something a bit more like the backup regime that I have for other systems:

  • A backup is taken every day, but these are only kept for 1 week, i.e. the Monday config export would be overwritten every Monday, the Tuesday overwritten every Tuesday etc.
  • Additionally, a backup is taken every Monday, but this time kept for 1 week, and the files are recycled every month, giving me Week1, Week2 etc.
  • Additionally, a backup is taken on the 1st of every month, and these backups are kept for 1yr before being recycled.

I can work out how to do the “1st of the Month” bit, but can’t find anything in the help.mikrotik or wiki.mikrotik on how to get the Weekday out of the date.

If it’s not possible to get Weekday, then I will just edit the script to do a daily backup each day of the month and then one on the first of the month and recycle these files acordngly, as I can work out how to do this from what I’ve already got.

Any help gratefully received.

Thanks

Colin

http://forum.mikrotik.com/t/i-did-it-script-to-compute-unix-time/68576/1

You must calculate the weekday starting from one fixed date you know the right value, and calculating the day passed since that date,
including for each month add the right number of days, and 29 feb when is needed…

{
/system clock
:local strDate  [get date]
:local intYear  [:tonum [:pick $strDate 7 11]]
:local strMonth [:pick $strDate 1 3]
:local intDay   ([:tonum [:pick $strDate 4 6]] - 1)

:local arrPreMonDays {an=0;eb=31;ar=59;pr=90;ay=120;un=151;ul=181;ug=212;ep=243;ct=273;ov=304;ec=334}
# 1970-01-01 is a Thursday
:local arrWeekDays   {"Thu";"Fri";"Sat";"Sun";"Mon";"Tue";"Wed"}
# first bixestile year just before 1970 is 1968
:local numTotalDays  (($intYear - 1968) / 4)
:if ((($intYear - 1968) % 4) = 0) do={:set ($arrPreMonDays->"an") -1; :set ($arrPreMonDays->"eb") 30}
:set numTotalDays  ($numTotalDays + (($intYear - 1970) * 365))
:set numTotalDays  ($numTotalDays + ($arrPreMonDays->$strMonth))
:set numTotalDays  ($numTotalDays + $intDay)
:local strWeekDay ($arrWeekDays->($numTotalDays % 7))
:put "Week Day is $strWeekDay"
}

(EDIT later: upgraded version of this script http://forum.mikrotik.com/t/changing-the-mmm-dd-yyyy-date-format/5183/10 )

This calculation isn’t accurate before Jan/01/1970, because I took this snippet from the code that calculates the unix epoch,
and it isn’t accurate after Feb/28/2100 because I didn’t consider the correction for leap day on century.


REFRESHED for 7.10+
http://forum.mikrotik.com/t/date-parts-weekday/162769/1
Full version of this script: http://forum.mikrotik.com/t/changing-the-mmm-dd-yyyy-date-format/5183/10

Hi,

The forum administrators should enable a like on each post to let the authors know that we like (and like a lot) these helpful posts.

Thank you for your contributions @rextended, @Amm0, @eworm, @Jotne, etc..

BR.

There is a button for this. Next to the post, see Checkmark icon

Thanks very much everyone.
Very helpful as always.

Colin

I don’t see it :frowning:

BR.

In “ProSilver” I can Quote or Report a post, not like.
.
post.png

+1

BR.

Hi @rextended,

Since the release of ROS v7.10 the date changed to 2023-07-02 format. So this function should change the months array, right?
Would it be something like this?

/system clock
:local strDate  [get date]
:local intYear  [:pick $strDate 0 4]
:local strMonth [:pick $strDate 5 7]
:local intDay   [:pick $strDate 8 10]

:local arrPreMonDays {01=0;02=31;03=59;04=90;05=120;06=151;07=181;08=212;09=243;10=273;11=304;12=334}
# 1970-01-01 is a Thursday
:local arrWeekDays   {"Thu";"Fri";"Sat";"Sun";"Mon";"Tue";"Wed"}
# first bixestile year after 1970 is 1972
:local numTotalDays  (($intYear - 1972) / 4)
:if ((($intYear - 1972) % 4) = 0) do={:set ($arrPreMonDays->01) -1; :set ($arrPreMonDays->02) 30}
:set numTotalDays  ($numTotalDays + (($intYear - 1970) * 365))
:set numTotalDays  ($numTotalDays + ($arrPreMonDays->$strMonth))
:set numTotalDays  ($numTotalDays + $intDay)
:local strWeekDay  ($arrWeekDays->($numTotalDays % 7))
:put "Week Day is $strWeekDay"

I have tested it and it works fine.

BR.

The full version, which also indicates, for example, the day of the year (from 1 to 365/366), how many days there are in the month (from 28 to 31),
current day of week number from 1 (mon) to 7 (sun), if the yoear is a leap year or not, etc...

This work regardless the RouterOS version 6, 7 and 7.10:

:global getweekday do={
    :local vdate     $1
    :local prMntDays [:toarray "0,0,31,59,90,120,151,181,212,243,273,304,334"]
    :local daysOnMnt [:toarray "0,31,28,31,30,31,30,31,31,30,31,30,31"]
    :local vdoff     [:toarray "0,4,5,7,8,10"]
    :local M         [:tonum [:pick $vdate ($vdoff->2) ($vdoff->3)]]
    :if ($vdate ~ ".../../....") do={
        :set vdoff [:toarray "7,11,1,3,4,6"]
        :set M     ([:find "xxanebarprayunulugepctovecANEBARPRAYUNULUGEPCTOVEC" [:pick $vdate ($vdoff->2) ($vdoff->3)] -1] / 2)
        :if ($M>12) do={:set M ($M - 12)}
    }
    :local yyyy [:pick $vdate ($vdoff->0) ($vdoff->1)]
    :if ((($yyyy - 1968) % 4) = 0) do={:set ($prMntDays->1) -1; :set ($prMntDays->2) 30; :set ($daysOnMnt->2) 29}
    :local totd ((($yyyy - 1970) * 365) + (($yyyy - 1968) / 4) + ($prMntDays->$M) + ([:tonum [:pick $vdate ($vdoff->4) ($vdoff->5)]] - 1))
    :return ([:toarray "Thu,Fri,Sat,Sun,Mon,Tue,Wed"]->($totd % 7))
}

:put [$getweekday [/sys clock get date]]
1 Like

Oh, very complete. Thanks Rex!!
Happy summer!

Thanks, also to you.

On the previous post “extracted function code” is missing one “:tonum”: fixed.