Working with dates and time

MaviBot works with the following DATE and TIME formats:

for dates – "dd.mm.yyyy" for time – "HH:MM"

Variables

To work with dates and time, you can use the following variables:

current_date - current date in format dd.mm.yyyy, based on project time zone next_day - tomorrow's date in format dd.mm.yyyy; useful for scheduling messages current_time - current time in format hh:mm, based on project time zone weekday - day of week as number: Monday = 1, Tuesday = 2, etc.

Quick access to function descriptions:

How to add time to time

You can compare dates and times using logical operators just like numbers.

addYear() | addMonth() | addDays() | addMinutes()

Description

addYear(date, years_to_add) - calculates new date by adding specified number of years to given date, taking leap years into account. Use negative number to subtract years.

addMonth(date, months_to_add) - calculates new date by adding specified number of months to given date, taking leap years into account. Use negative number to subtract months.

addDays(date, days_to_add) - adds specified number of days to date. Use negative number to subtract days. Example: addDays(current_date, 20) or addDays('20.08.2019', -99)

addMinutes(time, minutes_to_add) - adds specified number of minutes to time. Use negative number to subtract minutes. Example: addMinutes(current_time, 20) or addMinutes('20:08', -30)

'date' + days - adds specified number of days to date

'time' + minutes - adds specified number of minutes to time

Examples

Example of adding a certain number of years to a date:

Example of adding a certain number of months to a date:

Example of using a simple math operation — adding days to a date and minutes to a time:

Code example for copying

count = 2 date = '29.02.2020' x = addYear(date, count) y = addYear('31.12.2021', -1*count)

count = 10 date = '30.04.2019' x = addMonth(date, count) y = addMonth('31.12.2020', -1*count)

count = 10 date = '30.04.2019' x = addMonth(date, count) y = addMonth('31.12.2020', -1*count)

How to compare time

Description

You can compare dates and times using logical operators just like numbers: greater than > , less than < equal to == , not equal to != greater than or equal to >= , less than or equal to <=

Comparison conditions can also be combined using logical operators AND and OR.

Comparison conditions are entered in the "Variable" field.

Be sure to use delays when specifying comparison conditions in an arrow.

Example

'01.09.2020' < '11.10.2020'

'11.10.2020' >= current_date

current_date == date

current_time >= '18:00'

current_time <= '21:00'

How to set time intervals

Description

A very useful function for working with time intervals is:

time_interval('start_time', 'end_time')

The function returns logical True or False.

Use this function in blocks as a condition in the "Variable to compare" field or within a calculator using IF().

The function allows you to check whether the current time falls within the specified interval.

Example

time_interval('10:00','19:00') - checks time interval from 10 AM to 7 PM

time_interval('19:00','07:00') - checks time interval from 7 PM to 7 AM

time_interval('18.10.2021 10:00','25.10.2021 23:59:59') - checks time interval between different dates

time_interval('#{current_date} 10:00','#{date} 19:00') - checks time interval set using variables

Let’s take a closer look at the first example. The function reads as follows:

If the user sends a message containing the word "time" during the period from 10:00 to 19:00, the condition returns true; otherwise, it returns false.

This allows you to create different responses depending on the time of the client's request. For example, if the current time is between 10:00 and 19:00, the client will receive: "A manager will respond to you soon."

At any other time, they will receive: "The manager is currently unavailable; you will receive a reply during working hours from 10:00."

In the Calculator field, enter:

response = if(time_interval('10:00','19:00'), "A manager will respond to you soon", "The manager is currently unavailable; you will receive a reply during working hours from 10:00")

In the "Message" field, enter: #{response}

Code example for copying

response = if(time_interval('10:00','19:00'), "A manager will respond to you soon", "The manager is currently unavailable; you will receive a reply during working hours from 10:00")

How to find out how much time is left until day D

Example

To calculate how much time is left until day D:

Define the date and time of the target moment you want to count down to.

date_D = 10.10.2021 time_D = 12:00

Convert it to timestamp format:

timestampD = convert_datetime("#{date_D} #{time_D}", "%d.%m.%Y %H:%M", "%s")

Capture the current time in timestamp format:

timestamp0 = #{timestamp}

Round the obtained values to whole numbers:

timeD = int(#{timestampD}) time0 = int(#{timestamp0})

Calculate the difference in seconds:

time = timeD - time0

Divide by the number of seconds in a day, and round the result:

full_days = time/86400

days = int(#{full_days})

Calculate how many seconds remain from the partial day:

c = days*86400

d = time - c

Calculate how many hours this corresponds to and round the value:

e = d/3600 hours = int(e)

Calculate how many seconds remain from the partial hours:

g = hours*3600

h = d - g

Convert this to minutes and round the value:

i = h/60

minutes = int(i)

Calculate how many seconds remain:

k = minutes*60

seconds = h - k

And display the time in the "Message text" field:

Time remaining until start: #{days} days #{hours} hours #{minutes} minutes and #{seconds} seconds

Here’s the code to insert into the "Calculator" field:

The Calculator also supports subtracting dates and times without type conversion. For example:

'11.12.2021' - '8.12.2021' returns 3 (days)

'11:12' - '3:45' returns 447 (minutes)

How to find out the next birthday

Description

To have the bot send birthday greetings, you can use the function:

birthdate(date) - returns the next upcoming birthday based on the person's date of birth: reminder = birthdate('28.04.1994')

How to get the date of a specific weekday

Description

If you have recurring mailings on specific weekdays, you can get the date of a weekday to schedule the mailing.

weekday_date(weekday, b) - returns the date of the nearest specified weekday. It takes two parameters:

The first parameter is the weekday number to find (from 1 to 7, where 1 = Monday, 7 = Sunday).

The second parameter indicates whether to return today's date if the specified weekday is today. This parameter is optional and defaults to False.

Let's look at an example to understand how the second parameter works:

You want to schedule a message to be sent on Thursday.

But what if the client just registered on Thursday?

Should the message be sent this Thursday or the next one?

The second parameter set to False means the message will be sent only on the next Thursday (skipping today).

The second parameter set to True means the message will be sent this Thursday, if today is Thursday (including the registration day).

In our example, the user registers on Thursday, 13.02.2025, and we want to send the message on the next Thursday. So, the returned value will be "20.02.2025".

For instance, weekday_date(4) will return '20.02.2025'.

If you want the current day to count only up to a certain time, you can use this construction:

weekday_date(4, current_time < '13:00'), where the condition can be replaced with any condition you need.

Here’s an example of how to use the function:

Example of using the function in the "Send date" field of a connection

How to get the date of a specific day of month

Description

If you have recurring actions on a specific day of the month, you can get the date for the nearest occurrence of that day to schedule your mailing.

month_date(date, b) - returns the nearest date of the month for the specified day number. It takes two parameters:

date - day number of month to find (from 1 to 31)

b - optional parameter; indicates whether to return today’s date if it matches the specified day. Defaults to False.

If you pass 31 but the current month has only 30 days, the function will return the last day of the month (e.g., the 30th).

For example:

If today is 05.04.2022 and you call t = month_date(1), the result will be 01.05.2022 - the nearest 1st of the month after today.

How to check if the current time is within working hours

Description

For example, the company works from 9:00 AM to 6:00 PM and is closed on Saturdays and Sundays. Let's build the checks step by step:

  1. Check if the time is within working hours: current_time >= '9:00' AND current_time <= '18:00'

  2. Check if the time is outside working hours: current_time < '9:00' OR current_time > '18:00'

  3. Check if today is a working day (not Saturday or Sunday): weekday != 6 AND weekday != 7

  4. Combine conditions for writing during working hours: current_time >= '9:00' AND current_time <= '18:00' AND weekday != 6 AND weekday != 7

  5. Combine conditions for writing outside working hours: current_time < '9:00' OR current_time > '18:00' OR weekday == 6 OR weekday == 7

Example

How to convert date and time

Example

The date-time format used varies across systems, and it's possible that one of the integrated resources will return a time in a format that MaviBot doesn't recognize. You will need to convert it.

convert_datetime(date, fin, f_out) - converts date and time

There are three parameters:

  • Input date string

  • Input format string

  • Output format string

Example: convert_datetime("2011-11-03", "%Y-%m-%d", "%Y/%m/%d") на выходе будет 2011/11/03

This function converts the incoming date into a timestamp.

#{current_date} #{current_time} - converts to "06.01.2025 11:45" — since there is no information about seconds or microseconds, the resulting timestamp has zeros after the decimal point.

For example: 1736144700.000000

If you include milliseconds after the decimal point,

convert_datetime("06.01.2025 19:26:35.123456", "%d.%m.%Y %H:%M:%S.%f", "%s.%f")

the result will be: 1736166395.123456

get_datetime(format) - a function for getting the current time in a specific format. The function takes one parameter — a string that specifies the desired output format.

Important! The function's response will be (Monday, Tuesday, etc.). Example: get_datetime("%A") the output will be Wednesday

Description of the format string parameters, which are specified in the function as strings enclosed in quotation marks:

%a

Short name of weekday

Sun, Mon ...

%A

Full name of weekday

Monday, Tuesday, …, Sunday

%w

Day of the week as a decimal number, where 0 is Sunday and 6 is Saturday

0, 1, …, 6

%d

Day of the month as a zero-padded decimal number

01, 02, …, 31

%b

Short name of month

Jan, Feb, …, Dec

%B

Full name of month

January, February, March, etc.

%m

Month as a zero-padded decimal number

01, 02, …, 12

%y

Year as a two-digit number

00, 01, …, 99

%Y

Year as a four-digit number

0001, 0002, …, 2013, 2014, …, 9998, 9999

%H

Hour (24-hour clock) as a zero-padded two-digit number

00, 01, …, 23

%I

Hour (12-hour clock) as a zero-padded two-digit number

01, 02, …, 12

%p

AM or PM designator

AM, PM (en_US);am, pm (de_DE)

%M

Minutes as a zero-padded two-digit number

00, 01, …, 59

%S

Seconds as a zero-padded two-digit number

00, 01, …, 59

%f

Microseconds as a zero-padded six-digit number

000000, 000001, …, 999999

%z

UTC offset in the form ±HHMM[SS[.ffffff]] (empty string if the object is naive).

(empty), +0000, -0400, +1030, +063415, -030712.345216

%Z

Time zone name (empty string if the object is naive).

(empty), UTC, GMT

%j

Day of the year as a zero-padded three-digit number

001, 002, …, 366

%U

Week number of the year (with Sunday as the first day of the week) as a zero-padded decimal number. All days of the new year preceding the first Sunday are considered week zero.

00, 01, …, 53

%W

Week number of the year (with Monday as the first day of the week) as a decimal number. All days of the new year preceding the first Monday are considered week zero.

00, 01, …, 53

%%

Symbol '%'

%

%s

timestamp

1607926200

All other symbols represent themselves.

You can get the current timestamp using: convert_datetime("#{current_date} #{current_time}", "%d.%m.%Y %H:%M", "%s")

Display current date in the "dd month name" format

Example

To display the date in the "dd month name" format, use the function current_date_eng().

If you need to add a certain number of days to this date, you should specify the required number of days as a parameter in the function.

For example, if today’s date is April 3rd, then the function will return:

current_date_eng() - April 3rd

current_date_eng(2) - April 5th

current_date_eng(-2) - April 1st

Last updated