Compare dates

Hi gyus.How can I compare two dates? First is get by method ([/system clock get date]) and second is current date.
Result: 18/apr/2011 <> 24/jan/2012 | and after compare this dates:

  1. if Date1 is lower than current date → disable firewall rule
  2. if Date1 equal to current date → disable firewall rule
  3. if Date1 higher than current date → Enable firewall rule

I will run this compare every 4h so the script must be infallible and without bugs.Thank you for eventually help!

compare the comment of firewall rules and [system clock get date] ???

compare only two dates and disable or enable firewall rule based on result! first date is in format “15/mar/2011” second date is current date in same format.

Just a couple points of clarification.

  1. When I do [/system clock get date], the format I see is: apr/13/2012, not 13/apr/2012. Are you seeing it differently?
  2. And as long as you’re router is set correctly, [/system clock get date] should be the current date. Your first post made it sound like the current date was different.

The following script assumes that [/system clock get date] is the “current date”, and the other date is the one to compare. If date1 is greater than date2 (current date), enable the firewall rule. Otherwise, disable it. You’ll have to edit the firewall rule lines near the bottom to fit your needs.


# the date to compare
:local date1 "apr/18/2011";

# get current date
:local date2 [ /system clock get date ];


# months array
:local months ("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec");

# extract month from date1
:local date1month [ :pick $date1 0 3 ];
# extract day
:local date1day [ :pick $date1 4 6 ];
# extract year
:local date1year [ :pick $date1 7 11 ];
# get position of our month in the array = month number
:local mm ([ :find $months $date1month -1 ] + 1);
# if month number is less than 10 (a single digit), then add a leading 0
:if ($mm < 10) do={
	:set date1month ("0" . $mm);
# otherwise, just set it as the number
} else={
	:set date1month $mm;
}
# combine year, month, and day to create a "number", ie: 20120413
:local date1value ($date1year . $date1month . $date1day);


# extract month from date2
:local date2month [ :pick $date2 0 3 ];
# extract day
:local date2day [ :pick $date2 4 6 ];
# extract year
:local date2year [ :pick $date2 7 11 ];
# get position of our month in the array = month number
:local mm ([ :find $months $date2month -1 ] + 1);
# if month number is less than 10 (a single digit), then add a leading 0
:if ($mm < 10) do={
    :set date2month ("0" . $mm);
# otherwise, just set date2month as the number
} else={
    :set date2month $mm;
}
# combine year, month, and day to create a "number", ie: 20120413
:local date2value ($date2year . $date2month . $date2day);

# if date1 value is greater than date2, enable the firewall rule
if ($date1value > $date2value) do={
	/ip firewall filter enable...
# otherwise, disable the firewall rule
} else={
	/ip firewall filter disable...
}

Hi skot
i trying to use your script in one of my scripts which doesnt work in for loops
Througs a error when hotspot commetns are not in perticular format . can you assist . below works if user amit comment section
has right format like apr/25/2020 apr/21/2020 365 but if it has some other string in comment like “expired”
error is apr/25/2020 apr/21/2020 365

:local a [/ip hotspot user get value-name=comment “amit”];
:local currDate [/system clock get date];
:local l [:len $a];
:local startDate [:pick $a 0 11];
:local validityDate [:pick $a 12 23];
:local interval [:pick $a 24 $l];
:local myfunc [:parse [/system script get datecheck source]];
:local dateCheck [$myfunc date1=$startDate date2=$currDate];
:local validityCheck [$myfunc date1=$validityDate date2=$currDate];
#:local user “109”;

return $validityCheck;

#return $startDate;
:if ($validityCheck = “equal”) do={
/ ip hotspot active remove [find user=jassi]; ip hotspot user disable [find name=jassi]
}