Script function for converting and shifting the standard date to OS Mikrotik
Posted: Sun Aug 22, 2021 5:16 pm
Script function for converting and shifting the standard date to OS MikroTik
Start variables:
* First mandatory variable in "aug/22/2021" or "08/22/2021" format.
* Second optional variable in -5 or 0 or 5 format. Specifies how many days forward or backward you want to move the total date as a result.
* Third optional variable in true or false format. The value of the "true" variable prevents you from changing the format of the month from text to number and from number to text.
Example of execution:
Start variables:
* First mandatory variable in "aug/22/2021" or "08/22/2021" format.
* Second optional variable in -5 or 0 or 5 format. Specifies how many days forward or backward you want to move the total date as a result.
* Third optional variable in true or false format. The value of the "true" variable prevents you from changing the format of the month from text to number and from number to text.
Code: Select all
:global FuncDateConvert do={
:local LocalDate $1
:local ChangeDay $2
:local NoConvertMonth $3
:local Day [:pick $LocalDate ([:find $LocalDate "/" -1] + 1) [:find $LocalDate "/" [:find $LocalDate "/" -1]]]
:local Month [:pick $LocalDate -1 [:find $LocalDate "/" -1]]
:local Year [:pick $LocalDate ([:find $LocalDate "/" [:find $LocalDate "/" -1]] + 1) [:len $LocalDate]]
:local ArrayMonth {"jan";"feb";"mar";"apr";"may";"jun";"jul";"aug";"sep";"oct";"nov";"dec"}
:local ArrayMonthDay {31;28;31;30;31;30;31;31;30;31;30;31}
:local MonthType "str"
# Check the input format and correct if necessary.
:if ( [:typeof $NoConvertMonth] = "nothing") do={:set $NoConvertMonth "false"}
:if ( [:typeof $ChangeDay] = "nothing") do={:set $ChangeDay 0}
:if ( [:typeof $ChangeDay] = "str") do={:set $ChangeDay [:tonum $ChangeDay]}
:if ( [:typeof $Day] = "str") do={:set $Day [:tonum $Day]}
:if ( [:typeof $Year] = "str") do={:set $Year [:tonum $Year]}
# Check the data format of the Month variable and correct if necessary.
:if ( $Month~"^[0-9]{1,2}\$") do={
:set $Month [:tonum $Month]
:set $MonthType "num"
}
# Calculate the numerical equivalent for the Month variable
:if ($MonthType = "str") do={
:foreach MonthNum,MonthStr in=$ArrayMonth do={
:if ($MonthStr = $Month) do={:set $Month ($MonthNum + 1)}
}
}
# Calculate date shift by specified number of days
:if ($ChangeDay > 0) do={
:set $Day ($Day + $ChangeDay)
:while ((($Day > ($ArrayMonthDay->($Month - 1))) and ((($Year % 4) != 0) or ((($Year % 4) = 0) and ($Month != 2)))) or \
(($Day > (($ArrayMonthDay->($Month - 1)) + 1)) and ($Month = 2) and (($Year % 4) = 0))) do={
:if (($Month = 2) and (($Year % 4) = 0)) do={:set $Day ($Day - (($ArrayMonthDay->($Month - 1)) + 1))
} else={:set $Day ($Day - ($ArrayMonthDay->($Month - 1)))}
:set $Month ($Month + 1)
:if ($Month > 12) do={:set $Year ($Year + 1); :set $Month 1}
}
}
:if ($ChangeDay < 0) do={
:set $Day ($Day + $ChangeDay )
:while ($Day < 1) do={
:set $Month ($Month - 1)
:if ($Month < 1) do={:set $Year ($Year - 1); :set $Month 12}
:if (($Month = 2) and (($Year % 4) = 0)) do={:set $Day ($Day + (($ArrayMonthDay->($Month - 1)) + 1))
} else={:set $Day ($Day + ($ArrayMonthDay->($Month - 1)))}
}
}
# Add the character 0 to the beginning of the Day variable
:if (($Day < 10) and ([:len $Day] < 2)) do={:set $Day ("0".$Day)}
# Calculate the text equivalent for the Month variable
:if (($MonthType = "num") and ($NoConvertMonth = "false")) do={:set $Month ($ArrayMonth->($Month - 1))}
# Add the character 0 to the beginning of the Month variable
:if (($MonthType = "num") and ($NoConvertMonth = "true") and ($Month < 10)) do={:set $Month ("0".$Month)}
# Calculate the text equivalent for the Month variable
:if (($MonthType = "str") and ($NoConvertMonth = "true")) do={:set $Month ($ArrayMonth->($Month - 1))}
# Add the character 0 to the beginning of the Month variable
:if (($MonthType = "str") and ($NoConvertMonth = "false") and ($Month < 10)) do={:set $Month ("0".$Month)}
# Return data array in MM/DD/YYYY, DD, MM, YYYY format
:return {($Month."/".$Day."/".$Year);$Day;$Month;$Year}
}
Example of execution:
Code: Select all
:put "Start date 01/01/2021"
:put ([$FuncDateConvert "01/01/2021" -1 true]->0)
:put ([$FuncDateConvert "01/01/2021" -30 true]->0)
:put ([$FuncDateConvert "01/01/2021" -31 true]->0)
:put ([$FuncDateConvert "01/01/2021" -32 true]->0)
:put ([$FuncDateConvert "01/01/2021" 1 true]->0)
:put ([$FuncDateConvert "01/01/2021" 20 true]->0)
:put ([$FuncDateConvert "01/01/2021" 31 true]->0)
:put ([$FuncDateConvert "01/01/2021" 32 true]->0)
:put "Start date 11/01/2021"
:put ([$FuncDateConvert "11/01/2021" 1 true]->0)
:put ([$FuncDateConvert "11/01/2021" 20 true]->0)
:put ([$FuncDateConvert "11/01/2021" 31 true]->0)
:put ([$FuncDateConvert "11/01/2021" 32 true]->0)
:put ([$FuncDateConvert "11/01/2021" 61 true]->0)
:put ([$FuncDateConvert "11/01/2021" 62 true]->0)
:put ([$FuncDateConvert "11/01/2021" 91 true]->0)
:put ([$FuncDateConvert "11/01/2021" 92 true]->0)
Start date 01/01/2021
12/31/2020
12/02/2020
12/01/2020
11/30/2020
01/02/2021
01/21/2021
02/01/2021
02/02/2021
Start date 11/01/2021
11/02/2021
11/21/2021
12/02/2021
12/03/2021
01/01/2022
01/02/2022
01/31/2022
02/01/2022
:put "Start date 01/01/2021"
:put ([$FuncDateConvert "01/01/2021" -1 ]->0)
:put ([$FuncDateConvert "01/01/2021" -30 ]->0)
:put ([$FuncDateConvert "01/01/2021" -31 ]->0)
:put ([$FuncDateConvert "01/01/2021" -32 ]->0)
:put ([$FuncDateConvert "01/01/2021" 1 ]->0)
:put ([$FuncDateConvert "01/01/2021" 20 ]->0)
:put ([$FuncDateConvert "01/01/2021" 31 ]->0)
:put ([$FuncDateConvert "01/01/2021" 32 ]->0)
:put "Start date 11/01/2021"
:put ([$FuncDateConvert "11/01/2021" 1 ]->0)
:put ([$FuncDateConvert "11/01/2021" 20 ]->0)
:put ([$FuncDateConvert "11/01/2021" 31 ]->0)
:put ([$FuncDateConvert "11/01/2021" 32 ]->0)
:put ([$FuncDateConvert "11/01/2021" 61 ]->0)
:put ([$FuncDateConvert "11/01/2021" 62 ]->0)
:put ([$FuncDateConvert "11/01/2021" 91 ]->0)
:put ([$FuncDateConvert "11/01/2021" 92 ]->0)
Start date 01/01/2021
dec/31/2020
dec/02/2020
dec/01/2020
nov/30/2020
jan/02/2021
jan/21/2021
feb/01/2021
feb/02/2021
Start date 11/01/2021
nov/02/2021
nov/21/2021
dec/02/2021
dec/03/2021
jan/01/2022
jan/02/2022
jan/31/2022
feb/01/2022
:put "Start date 01/01/2021"
:put ([$FuncDateConvert "jan/01/2021" -1 ]->0)
:put ([$FuncDateConvert "jan/01/2021" -30 ]->0)
:put ([$FuncDateConvert "jan/01/2021" -31 ]->0)
:put ([$FuncDateConvert "jan/01/2021" -32 ]->0)
:put ([$FuncDateConvert "jan/01/2021" 1 ]->0)
:put ([$FuncDateConvert "jan/01/2021" 20 ]->0)
:put ([$FuncDateConvert "jan/01/2021" 31 ]->0)
:put ([$FuncDateConvert "jan/01/2021" 32 ]->0)
:put "Start date 11/01/2021"
:put ([$FuncDateConvert "nov/01/2021" 1 ]->0)
:put ([$FuncDateConvert "nov/01/2021" 20 ]->0)
:put ([$FuncDateConvert "nov/01/2021" 31 ]->0)
:put ([$FuncDateConvert "nov/01/2021" 32 ]->0)
:put ([$FuncDateConvert "nov/01/2021" 61 ]->0)
:put ([$FuncDateConvert "nov/01/2021" 62 ]->0)
:put ([$FuncDateConvert "nov/01/2021" 91 ]->0)
:put ([$FuncDateConvert "nov/01/2021" 92 ]->0)
Start date 01/01/2021
12/31/2020
12/02/2020
12/01/2020
11/30/2020
01/02/2021
01/21/2021
02/01/2021
02/02/2021
Start date 11/01/2021
11/02/2021
11/21/2021
12/02/2021
12/03/2021
01/01/2022
01/02/2022
01/31/2022
02/01/2022