Changing the MMM/DD/YYYY date format.

Hi!

I have a problem. I’m creating a automated backup system, and i need to save files attached to an email. And it would be nice if the router could name the file: inentity/date/time.

The problem occures when adding in the backup name [/system clock get date] - because of “/” marks. Mabey someone has a tip how to change the date format to MMMDDYYYY.. It can be done with regular expressions, but I can’t find if RouterOS supports them.

Thanks for any help.

Regards
Michal Podoski

You can try this:

:local d [/system clock get date]
:local p1 [:find $d “/”]
:local p2 [:find $d “/” $p1+1]
:local mydate ([:pick $d 0 $p1] . “.” . [:pick $d ($p1 + 1) $p2] . “.” . [:pick $d ($p2 + 1) ($p2 + 5)])
:put $mydate

If you are using script to backup with you can add that into it and use mydate variable as necessary. You could get away with stuffing all that onto a single command and not using vars - stepping thru it with vars shows you how its done though.

Sam

Hi!

It don’t seem like I have in 2.8 a ICE command called :pick.

I solved the problem this way:

  1. backup_start:

/system backup save
/system script run [/system script find name=backup_proceed]

  1. backup_proceed

:delay 10s
:global nazwa
:set nazwa ([/file get [/file find type=backup] name])
:global backup
:set backup ([/system identity get name] . “_” . $nazwa)
/file remove [/file find type=backup]
/system backup save name=($backup)
/system script run [/system script find name=backup_mail]

  1. backup_mail
    :delay 10s
    /tool e-mail send
    from=([/system identity get name] . “@.mydomain”)
    to=“michalp@mydomain”
    subject=($backup)
    file=([/file find type=backup])
    /file remove [/file find type=backup]
    /system script run [/system script find name=backup_clear]

  2. backup_clear

:unset backup
:unset nazwa

And it works:))))

Regards
Michal Podoski

it is that a question?

the routeros 2.9.2 the default form is that!!

In one line:

Month.Day.Year:
:put [([:pick [/system clock get date] ([:find [/system clock get date] “/”] + 1) [:find [/system clock get date] “/” ([:find [/system clock get date] “/” ] +1)]] . “.” . [:pick [/system clock get date] 0 [:find [/system clock get date] “/”]] . “.” . [:pick [/system clock get date] ([:find [/system clock get date] “/” ([:find [/system clock get date] “/” ] +1)] + 1) ([:find [/system clock get date] “/” ([:find [/system clock get date] “/” ] +1)] + 5)])]

Day.Month.Year:
:put [([:pick [/system clock get date] ([:find [/system clock get date] “/”] + 1) [:find [/system clock get date] “/” ([:find [/system clock get date] “/” ] +1)]] . “.” . [:pick [/system clock get date] 0 [:find [/system clock get date] “/”]] . “.” . [:pick [/system clock get date] ([:find [/system clock get date] “/” ([:find [/system clock get date] “/” ] +1)] + 1) ([:find [/system clock get date] “/” ([:find [/system clock get date] “/” ] +1)] + 5)])]

Year Month Day:
:put [([:pick [/system clock get date] ([:find [/system clock get date] “/” ([:find [/system clock get date] “/” ] +1)] + 1) ([:find [/system clock get date] “/” ([:find [/system clock get date] “/” ] +1)] + 5)] . “.” . [:pick [/system clock get date] 0 [:find [/system clock get date] “/”]] . “.” . [:pick [/system clock get date] ([:find [/system clock get date] “/”] + 1) [:find [/system clock get date] “/” ([:find [/system clock get date] “/” ] +1)]])]

Trigger Script with YearMonthDay.backup as name:
/system backup save name=([([:pick [/system clock get date] ([:find [/system clock get date] “/” ([:find [/system clock get date] “/” ] +1)] + 1) ([:find [/system clock get date] “/” ([:find [/system clock get date] “/” ] +1)] + 5)] . “.” . [:pick [/system clock get date] 0 [:find [/system clock get date] “/”]] . “.” . [:pick [/system clock get date] ([:find [/system clock get date] “/”] + 1) [:find [/system clock get date] “/” ([:find [/system clock get date] “/” ] +1)]])].backup)

Finally, script to push your daily backup to an external FTP server while maintaining dates:

/tool fetch address=ADDRESS port=PORT user=USER mode=ftp password=PASSWORD src-path=([([:pick [/system clock get date] ([:find [/system clock get date] “/” ([:find [/system clock get date] “/” ] +1)] + 1) ([:find [/system clock get date] “/” ([:find [/system clock get date] “/” ] +1)] + 5)] . “.” . [:pick [/system clock get date] 0 [:find [/system clock get date] “/”]] . “.” . [:pick [/system clock get date] ([:find [/system clock get date] “/”] + 1) [:find [/system clock get date] “/” ([:find [/system clock get date] “/” ] +1)]])].“.backup”) dst-path=(“/REMOTE/FTP/PATH/”.([([:pick [/system clock get date] ([:find [/system clock get date] “/” ([:find [/system clock get date] “/” ] +1)] + 1) ([:find [/system clock get date] “/” ([:find [/system clock get date] “/” ] +1)] + 5)] . “.” . [:pick [/system clock get date] 0 [:find [/system clock get date] “/”]] . “.” . [:pick [/system clock get date] ([:find [/system clock get date] “/”] + 1) [:find [/system clock get date] “/” ([:find [/system clock get date] “/” ] +1)]])].“.backup”)) upload=yes;

Thanks man for the Epic thread mining.
That has actually helped me a lot with some FTP scripts, your a champion.

format YYYY-MM-DD

:local date;
:set date ([:pick [/system clock get date] ([:find [/system clock get date] "/" ([:find [/system clock get date] "/" ] +1)] + 1) ([:find [/system clock get date] "/" ([:find [/system clock get date] "/" ] +1)] + 5)]);
:if ("jan"=[:pick [/system clock get date] 0 [:find [/system clock get date] "/"]]) do={
  :set date ($date."-01-"); 
} else={
  :if ("feb"=[:pick [/system clock get date] 0 [:find [/system clock get date] "/"]]) do={
    :set date ($date."-02-"); 
  } else={
    :if ("mar"=[:pick [/system clock get date] 0 [:find [/system clock get date] "/"]]) do={
      :set date ($date."-03-");
    } else={
      :if ("apr"=[:pick [/system clock get date] 0 [:find [/system clock get date] "/"]]) do={
        :set date ($date."-04-");
      } else={
        :if ("may"=[:pick [/system clock get date] 0 [:find [/system clock get date] "/"]]) do={
          :set date ($date."-05-");
        } else={
          :if ("jun"=[:pick [/system clock get date] 0 [:find [/system clock get date] "/"]]) do={
            :set date ($date."-06-");
          } else= {
            :if ("jul"=[:pick [/system clock get date] 0 [:find [/system clock get date] "/"]]) do={
              :set date ($date."-07-");
            } else={
              :if ("aug"=[:pick [/system clock get date] 0 [:find [/system clock get date] "/"]]) do={
                :set date ($date."-08-");
              } else={
                :if ("sep"=[:pick [/system clock get date] 0 [:find [/system clock get date] "/"]]) do={
                  :set date ($date."-09-");
                } else={
                  :if ("oct"=[:pick [/system clock get date] 0 [:find [/system clock get date] "/"]]) do={
                    :set date ($date."-10-");
                  } else={
                    :if ("nov"=[:pick [/system clock get date] 0 [:find [/system clock get date] "/"]]) do={
                      :set date ($date."-11-");
                    } else={
                      :if ("dec"=[:pick [/system clock get date] 0 [:find [/system clock get date] "/"]]) do={
                        :set date ($date."-12-");
                      } else={
                        :set date ($date."-".([:pick [/system clock get date] 0 [:find [/system clock get date] "/"]])."-");
                      }
                    }
                  }
                }
              }
            }
          }
        } 
      }
    }
  } 
}
:set date ($date.[:pick [/system clock get date] ([:find [/system clock get date] "/"] + 1) [:find [/system clock get date] "/" ([:find [/system clock get date] "/" ] +1)]]);
# jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec

:put $date

Took this and turned into a function that can be called from any script on my MT if the script to create the function is run on boot.

#create the timestamp in format YYYY-MM-DD HH
# :global timestamp
#: local mytimevariable
#usage:  :set mytimevariable [$timestamp]

:global timestamp do={

:local currtime [/system clock get time]
:local currdate [/system clock get date]
:local curryr  ([:pick $currdate ([:find $currdate "/" ([:find $currdate "/" ] +1)] + 1) ([:find $currdate "/" ([:find $currdate "/" ] +1)] + 5)])
:local currmo ([:pick $currdate 0 [:find $currdate "/"]])
:local currday ([:pick $currdate ([:find $currdate "/"] + 1) [:find $currdate "/" ([:find $currdate "/" ] +1)]])
:local currhr  ([:pick $currtime 0 ([:find $currtime ":"])]) 
:local currmin ([:pick $currtime ([:find $currtime ":"] + 1) [:find $currtime ":" ([:find $currtime ":" ] +1)]])
:local currsec ([:pick $currtime ([:find $currtime ":" ([:find $currtime ":" ] +1)] + 1) ([:find $currtime ":" ([:find $currtime ":" ] +1)] + 5)])
:local datevar
:local month

:if ("jan"=$currmo) do={
  :set month ($month."-01-"); 
} else={
  :if ("feb"=$currmo) do={
    :set month ($month."-02-"); 
  } else={
    :if ("mar"=$currmo) do={
      :set month ($month."-03-");
    } else={
      :if ("apr"=$currmo) do={
        :set month ($month."-04-");
      } else={
        :if ("may"=$currmo) do={
          :set month ($month."-05-");
        } else={
          :if ("jun"=$currmo) do={
            :set month ($month."-06-");
          } else= {
            :if ("jul"=$currmo) do={
              :set month ($month."-07-");
            } else={
              :if ("aug"=$currmo) do={
                :set month ($month."-08-");
              } else={
                :if ("sep"=$currmo) do={
                  :set month ($month."-09-");
                } else={
                  :if ("oct"=$currmo) do={
                    :set month ($month."-10-");
                  } else={
                    :if ("nov"=$currmo) do={
                      :set month ($month."-11-");
                    } else={
                      :if ("dec"=$currmo) do={
                        :set month ($month."-12-");
                      } else={
                        :set month ($month."-".($currmo)."-");
                      }
                    }
                  }
                }
              }
            }
          }
        } 
      }
    }
  } 
}

:set datevar ($curryr.$month.$currday." ".$currhr); #format YYYY-MM-DD HH
#:set datevar ($curryr.$month.$currday." ".$currhr.":".$currmin.":".$currsec); #format YYYY-MM-DD HH:MM:SS
:return { $datevar };
}

Something… simpler?

simpler version: (compatible also with new date format on RouterOS 7.10+)

:global simplercurrdatetimestr do={
    /system clock
    :local vdate [get date]
    :local vtime [get time]
    :local vdoff [:toarray "0,4,5,7,8,10"]
    :local MM    [:pick $vdate ($vdoff->2) ($vdoff->3)]
    :local M     [:tonum $MM]
    :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)}
        :set MM    [:pick (100 + $M) 1 3]
    }
    :local yyyy [:pick $vdate ($vdoff->0) ($vdoff->1)]
    :local dd   [:pick $vdate ($vdoff->4) ($vdoff->5)]
    :local HH   [:pick $vtime 0  2]
    :local mm   [:pick $vtime 3  5]
    :local ss   [:pick $vtime 6  8]

    :return "$yyyy-$MM-$dd $HH:$mm:$ss"
}

:put [$simplercurrdatetimestr]

full version: (compatible also with new date format on RouterOS 7.10+)

:global currdatetimestr do={
    /system clock
    :local vdate [get date]
    :local vtime [get time]
    :local vgmt  [:tonum [get gmt-offset]]; :if ($vgmt > 0x7FFFFFFF) do={:set vgmt ($vgmt - 0x100000000)}
    :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 LcaseMnts [:toarray "0,jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec"]
    :local PcaseMnts [:toarray "0,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"]
    :local UcaseMnts [:toarray "0,JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC"]
    :local LcaseWeekDays [:toarray "thu,fri,sat,sun,mon,tue,wed"]
    :local PcaseWeekDays [:toarray "Thu,Fri,Sat,Sun,Mon,Tue,Wed"]
    :local UcaseWeekDays [:toarray "THU,FRI,SAT,SUN,MON,TUE,WED"]
    :local NumbrWeekDays [:toarray "4,5,6,7,1,2,3"]
    :local Fzerofill do={:return [:pick (100 + $1) 1 3]}
    :local gmtSg "+"; :if ($vgmt < 0) do={:set gmtSg "-"; :set vgmt ($vgmt * -1)}
    :local gmtHr [:pick [:totime $vgmt] 0 2]
    :local gmtMn [:pick [:totime $vgmt] 3 5]
    :local vdoff [:toarray "0,4,5,7,8,10"]
    :local MM    [:pick $vdate ($vdoff->2) ($vdoff->3)]
    :local M     [:tonum $MM]
    :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)}
        :set MM    [:pick (100 + $M) 1 3]
    }
    :local yyyy [:pick $vdate ($vdoff->0) ($vdoff->1)]
    :local Leap "No-Leap"
    :if ((($yyyy - 1968) % 4) = 0) do={:set Leap "Leap"; :set ($prMntDays->1) -1; :set ($prMntDays->2) 30; :set ($daysOnMnt->2) 29}
    :local mmm  ($LcaseMnts->$M)
    :local Mmm  ($PcaseMnts->$M)
    :local MMM  ($UcaseMnts->$M)
    :local MD   ($daysOnMnt->$M)
    :local dd   [:pick $vdate ($vdoff->4) ($vdoff->5)]
    :local d    [:tonum $dd] ; :local totd ((($yyyy - 1970) * 365) + (($yyyy - 1968) / 4) + ($prMntDays->$M) + ($d - 1))
    :local YD   (($prMntDays->$M) + $d)
    :local www  ($LcaseWeekDays->($totd % 7))
    :local Www  ($PcaseWeekDays->($totd % 7))
    :local WWW  ($UcaseWeekDays->($totd % 7))
    :local WD   ($NumbrWeekDays->($totd % 7))
    :local HH   [:pick $vtime 0  2]
    :local H    [:tonum $HH]
    :local hh   ([:tonum $HH] % 12); :if ($hh = 0) do={:set hh 12}; :set hh [$Fzerofill $hh]
    :local h    [:tonum $hh]
    :local a    "A"; :if ([:tonum $HH] > 11) do={:set a "P"}
    :local aa   "$a\4D"
    :local mm   [:pick $vtime 3  5]
    :local m    [:tonum $mm]
    :local ss   [:pick $vtime 6  8]
    :local s    [:tonum $ss]
    :local Z    "$gmtSg$gmtHr:$gmtMn"
    :local Unix (((((($totd * 24) + $H) * 60) + $m) * 60) + $s - $vgmt)
    :return "$yyyy-$MM-$dd\54$HH:$mm:$ss$Z $Www (YD: $YD) (MD: $MD) (WD: $WD) $Leap $Unix"
}

:put [$currdatetimestr]

On full version can be used on “return”:
$yyyy current year full number ex. 2022

$MM current month number with 0 ex. 01,02,03…10,11,12
$M current month number without 0 ex. 1,2,3…10,11,12

$MMM current short mont name UPPERCASE JAN,FEB…NOV,DIC
$Mmm current short mont name Propercase Jan,Feb…Nov,Dic (not standard)
$mmm current short mont name lowercase jan,feb…nov,dic (not standard)
$MD it indicates how many days a month is made up of

$dd current day number with 0 ex. 01,02,03…29,30,31
$d current day number without 0 ex. 1,2,3…29,30,31
$YD current day number of the year 1…365/366

$WWW current short day of week name UPPERCASE THU,FRI,SAT,SUN,MON,TUE,WED
$Www current short day of week name Propercase Thu,Fri,Sat,Sun,Mon,Tue,Wed (not standard)
$www current short day of week name lowercase thu,fri,sat,sun,mon,tue,wed (not standard)
$WD current day of week number from 1 (mon) to 7 (sun)

$HH current hour number 24h format with 0 ex. 00,01,02…21,22,23
$H current hour number 24h format without 0 ex. 0,1,2…21,22,23

$hh current hour number 12h format with 0 ex. 01,02,03…10,11,12
$h current hour number 12h format without 0 ex. 1,2,3…10,11,12

$aa current AM / PM symbol for 12h format ex. AM or PM
$a current first letter only of AM / PM symbol for 12h format ex. A or P

$mm current minute number with 0 ex. 00,01,02…57,58,59
$m current minute number without 0 ex. 0,1,2…57,58,59

$ss current second number with 0 ex. 00,01,02…57,58,59(,60?)
$s current second number without 0 ex. 0,1,2…57,58,59(,60?)

$Z current Time Zone offset ex. -23:59…+00:00…+23:59

$Leap if the current year is leap year (Leap) or not (No-Leap)

$Unix Epoch Unix Time

If desired, Lcase, Pcase and Ucase arrays can be localized, but only keeping English letters, uppercase or lowercase, from A to Z.

REX … why create a GLOBAL variable and not place it like this


{
    /system clock
    :local vdate [get date]
    :local vtime [get time]
    :local yyyy  [:pick $vdate 7 11]
    :local M     ([:find "xxanebarprayunulgoepctovecANEBARPRAYUNULGOEPCTOVEC" [:pick $vdate 1 3] -1]/2); :if ($M>12) do={:set M ($M - 12)}
    :local MM    $M; :if ($MM < 10) do={:set MM "0$MM"}
    :local dd    [:pick $vdate 4  6]
    :local HH    [:pick $vtime 0  2]
    :local mm    [:pick $vtime 3  5]
    :local ss    [:pick $vtime 6  8]

    :put "$yyyy-$MM-$dd $HH:$mm:$ss"
}




EL DONCITO.

Why not?

I ask because I want to learn more






EL DONCITO.

Finally…

If you have to do the same thing on multiple points in one script, or on multiple different scripts on RouterOS, writing a function is the logical thing to do.

Obviously, if you only have to use that procedure once, it doesn’t make sense to make a function out of it,
but if you need to do the same thing multiple times, because on this example you need everityme the correct current time, a function is better.

Good day @rextended

I use a backup script that includes the following to determine the filename, but due to the date change in 7.10, this obviously does not work. How would you suggest I fix it?

Set Local and Remote Filename variables. Do not change this unless you want to edit the format of the filename.

Default “local file name” is always the same to avoid lots of files and running out of space, “remote file name” uploaded to FTP has the date

:local hostname [/system identity get name]
:local date ([:pick [/system clock get date] 7 11]
. [:pick [/system clock get date] 0 3]
. [:pick [/system clock get date] 4 6]);
:local localfilename “$hostname-Backup-Daily”;
:local remotefilename “$hostname-$date”;

you have to stop copying and pasting, instead you have to think about what you do…

:local hostname [/system identity get name]
:local date [/system clock get date]
:local localfilename "$hostname-Backup-Daily";
:local remotefilename "$hostname-$date";