The function to obtain the date and time on RouterOS is very simple
:local date [/system clock get date] :local time [/system clock get time]
Bad news, the variable $date contains the value “Dec/25/2018” which is not very adapted to scripting operations. Then if you want to get a more adapted format, such as 2018-12-25, you have to manipulate the content of the variable :
:local date [/system clock get date] :local day [ :pick $date 4 6 ] :local month [ :pick $date 0 3 ] :local year [ :pick $date 7 11 ]
Now you have the good values in the three variables. But $month gives you “dec”, not “12”. You need to convert this element
:local date [/system clock get date]; :local months {"jan"="01";"feb"="02";"mar"="03";"apr"="04";"may"="05";"jun"="06";"jul"="07";"aug"="08";"sep"="09";"oct"=10;"nov"=11;"dec"=12}; :local day [:tonum [:pick $date 4 6]];:local year [:tonum [:pick $date 7 11]];:local month [:pick $date 0 3];:local mm (:$months->$month); :local newdate "$year-$mm-$day";
And that’s it, the newdate variable now contains “2018-12-25”.
The Mikrotik forums provides many examples of date manipulations. The following example provided by BlackVS (here) gives a good overview of some advances scripting possibilites :
###func_shiftDate - add days to date #Input: date, days #date - "jan/1/2017" #days - number #correct only for years >1918 #uncomment for testing #:local date "jan/01/2100" #:local days 2560 ### #:put "$date + $days"