Community discussions

MikroTik App
 
nemke
Member Candidate
Member Candidate
Topic Author
Posts: 160
Joined: Thu Jul 31, 2014 2:52 am

date,time format

Sun Sep 30, 2018 10:15 pm

Hello to all
for project I need special sintax in http get request to server. I use fetch tool on MT but I need time and date from MT in special format understandable for server like this:
date,time example 300918,202130 (this is how remote server understand)
Where:
300918 = 30.sep.2018.
202130 = 20h 21min 30sec

How to achave this ? MT use NTP for clock

[admin@xxxxxxxx] > system clock print
time: 21:10:31
date: sep/30/2018
time-zone-autodetect: yes
time-zone-name: Europe/Belgrade
gmt-offset: +02:00
dst-active: yes

[admin@xxxxxxxxxx] /system ntp client> print
enabled: yes
primary-ntp: 10.20.20.254
secondary-ntp: 0.0.0.0
server-dns-names:
mode: unicast
poll-interval: 15m
active-server: 10.20.20.254
last-update-from: 10.20.20.254
last-update-before: 55s310ms
last-adjustment: 13ms692us
 
User avatar
astounding
Member Candidate
Member Candidate
Posts: 121
Joined: Tue Dec 16, 2008 12:17 am

Re: date,time format

Fri Nov 16, 2018 2:09 pm

You mean like this?
{
  :local mydatetime
  :local time [/system clock get time]
  :local date [/system clock get date]
  :local month [:tostr ([:find \
    "janfebmaraprmayjunjulaugsepoctnovdec" [:pick $date 0 3] \
  ]/3+1)]
  :if ([:tonum $month]<10) do={
    :set month "0$month"
  }
  :set mydatetime ( \
    $month.[:pick $date 4 6].[:pick $date 9 11].",". \
    [:pick $time 0 2].[:pick $time 3 5].[:pick $time 6 8] \
  )
  :put "Current date/time in DDMMYY,HHMMSS format is: $mydatetime"
}
-Astounding
 
dhcpy
just joined
Posts: 12
Joined: Tue Oct 19, 2010 6:38 am

Re: date,time format

Wed Dec 26, 2018 11:11 pm

great code, thank you!!
You mean like this?
{
  :local mydatetime
  :local time [/system clock get time]
  :local date [/system clock get date]
  :local month [:tostr ([:find \
    "janfebmaraprmayjunjulaugsepoctnovdec" [:pick $date 0 3] \
  ]/3+1)]
  :if ([:tonum $month]<10) do={
    :set month "0$month"
  }
  :set mydatetime ( \
    $month.[:pick $date 4 6].[:pick $date 9 11].",". \
    [:pick $time 0 2].[:pick $time 3 5].[:pick $time 6 8] \
  )
  :put "Current date/time in DDMMYY,HHMMSS format is: $mydatetime"
}
-Astounding
 
luca1234567
newbie
Posts: 31
Joined: Tue May 15, 2018 1:27 am
Contact:

Re: date,time format

Sat Nov 28, 2020 6:02 pm

@Astounding
...
...
  :set mydatetime ( \
    $month.[:pick $date 4 6].[:pick $date 9 11].",". \
    [:pick $time 0 2].[:pick $time 3 5].[:pick $time 6 8] \
  )
  :put "Current date/time in DDMMYY,HHMMSS format is: $mydatetime"
}
With DDMMYY,HHMMSS format the code may be:
 :set mydatetime ( [:pick $date 4 6].$month.[:pick $date 9 11].",".[:pick $time 0 2].[:pick $time 3 5].[:pick $time 6 8] )

-------
I suggest a more light code, without division.
# BEGIN Script v2

# BEGIN Config
# END Config

:local Date [/system clock get date]
:local Time [/system clock get time]
:local Month [:tostr ([:find [:toarray "jan,feb,mar,apr,may,jun,jul,ago,sep,oct,nov,dec"] [:pick $Date 0 3]]+1)]
:put ("Month=".$Month)
:if (:len $Month<2) do={
  :set Month "0$Month"
 }
#To be sure
:set Month [:pick [$Month] 0 2]
:put ("Month=".$Month)

# Format YYYYMMDD-HHMMSS
:global DateTimeCode ([:pick $Date 7 11].$Month.[:pick $Date 4 6]."-".[:pick $Time 0 2].[:pick $Time 3 5].[:pick $Time 6 8])
# Format DDMMYY-HHMMSS
#:global DateTimeCode ([:pick $Date 4 6].$Month.[:pick $Date 9 11]."-".[:pick $Time 0 2].[:pick $Time 3 5].[:pick $Time 6 8])

:put ("DateTimeCode=" . $DateTimeCode)

# END Script
The :put, only for testing, may be removed when copy in Mikrotik script.
 
truefriendcz
newbie
Posts: 39
Joined: Mon Jun 05, 2017 11:07 am

Re: date,time format

Thu Feb 10, 2022 1:38 pm

@Astounding
...
...
  :set mydatetime ( \
    $month.[:pick $date 4 6].[:pick $date 9 11].",". \
    [:pick $time 0 2].[:pick $time 3 5].[:pick $time 6 8] \
  )
  :put "Current date/time in DDMMYY,HHMMSS format is: $mydatetime"
}
With DDMMYY,HHMMSS format the code may be:
 :set mydatetime ( [:pick $date 4 6].$month.[:pick $date 9 11].",".[:pick $time 0 2].[:pick $time 3 5].[:pick $time 6 8] )

-------
I suggest a more light code, without division.
# BEGIN Script v2

# BEGIN Config
# END Config

:local Date [/system clock get date]
:local Time [/system clock get time]
:local Month [:tostr ([:find [:toarray "jan,feb,mar,apr,may,jun,jul,ago,sep,oct,nov,dec"] [:pick $Date 0 3]]+1)]
:put ("Month=".$Month)
:if (:len $Month<2) do={
  :set Month "0$Month"
 }
#To be sure
:set Month [:pick [$Month] 0 2]
:put ("Month=".$Month)

# Format YYYYMMDD-HHMMSS
:global DateTimeCode ([:pick $Date 7 11].$Month.[:pick $Date 4 6]."-".[:pick $Time 0 2].[:pick $Time 3 5].[:pick $Time 6 8])
# Format DDMMYY-HHMMSS
#:global DateTimeCode ([:pick $Date 4 6].$Month.[:pick $Date 9 11]."-".[:pick $Time 0 2].[:pick $Time 3 5].[:pick $Time 6 8])

:put ("DateTimeCode=" . $DateTimeCode)

# END Script
The :put, only for testing, may be removed when copy in Mikrotik script.
I modify for better use (adding "-") but... how i can replace on 1-9 months before month number zero?
Example:
Now: 2022-2-15_-_10-32-16
I want: 2022-02-15_-_10-32-16
# BEGIN Script v2

# BEGIN Config
# END Config

:local Date [/system clock get date]
:local Time [/system clock get time]
:local Month [:tostr ([:find [:toarray "jan,feb,mar,apr,may,jun,jul,ago,sep,oct,nov,dec"] [:pick $Date 0 3]]+1)]
:put ("Month=".$Month)
:if (:len $Month<2) do={
  :set Month "0$Month"
 }
#To be sure
:set Month [:pick [$Month] 0 2]
:put ("Month=".$Month)

# Format YYYYMMDD-HHMMSS
:global DateTimeCode ([:pick $Date 7 11]."-".$Month."-".[:pick $Date 4 6]."_-_".[:pick $Time 0 2]."-".[:pick $Time 3 5]."-".[:pick $Time 6 8])
# Format DDMMYY-HHMMSS
#:global DateTimeCode ([:pick $Date 4 6].$Month.[:pick $Date 9 11]."-".[:pick $Time 0 2].[:pick $Time 3 5].[:pick $Time 6 8])

:put ("DateTimeCode=" . $DateTimeCode)

:log info message=[:put ($DateTimeCode)]

# END Script

Who is online

Users browsing this forum: No registered users and 9 guests