Working with Dates and Time

You can use date and time comparisons, for example, to get into working time

Salebot works with dates in the format "dd.mm.yyyy" and with time in the format "HH:MM"

Variables

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

#{current_date} - current date in dd.mm.yyyy format

#{next_day} - tomorrow’s date in the format dd.mm.yyyy. Convenient for sending messages tomorrow.

#{current_time} - Moscow time in the format hh:mm

#{weekday} - day of the week as a number, Monday - 1, Tuesday - 2 Quick access to function descriptions:

  • add time to the time

  • compare time

  • set time interval

  • to know how long until day D

  • to know your next birthday

  • get the day of the week

  • get the date of the month

  • check for hit during working hours

  • convert date and time

  • video lesson

How to add time to the time

You can compare dates and times by logical operators, as well as numbers.

addDays (date, day_add) - for addition to the date of the specified number of days. To subtract, pass a negative number. Example: addDays(current_date, 20) or addDays('20.08.2019', -99)

addMinutes (date, minute_add) adds the number of minutes to the specified time. To subtract, pass a negative number. Example: addMinutes(current_time, 20) or addMinutes('20:08', -30)

Or you can directly add days to dates and minutes to time.

Example:

'12.10.2021' + 3

'15:00' + 120

current_date + 2

current_time + 30

How to compare time

You can compare dates and times by logical operators, as well as numbers.

Examples:

'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

In order not to use complex constructions as in the previous screenshot, you can specify a time interval by using the function:

result = timeinterval('Time' start','Time' end')

where the result is True (condition met) or False (condition not met) Examples:

time_interval('10:00', '19:00') - check the time interval from 10 am to 7 pm

time_interval('19:00', '8:00') - check the time interval from 7 pm to 8 am

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

time_interval('#{current_date} 10:00', '#{date} 19:00') - check the time interval specified with variables

Then you will have the condition fulfilled within the set time frame.

Also this you can use in constructions with if in the field "Calculator"

Example: If a user writes the word "time" between 10 am and 7 pm, he will receive a reply "You will soon be answered by the manager", at another time he will receive a reply "Now the manager is not in place, you will be answered at 10:00 pm."

Write in the field "Calculator":

Answer = if(time_interval('10:00','19:00'), "The manager will answer you soon", "Now the manager is not in place, you will reply in the working time from 10:00")

in answer field - #{answer}

To find out how much time is left before day D:

To solve the problem how much time is left before day D , we need to set the date and time of the time until which we count the time:

date_D = 10.10.2021 time_D = 12:00

Convert to timestamp format:

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

Commit the current time in timestamp format:

timestamp0 = #{timestamp}

Round the obtained values to the whole:

timeD = int(#{timestampD})

time0 = int(#{timestamp0})

Calculate the difference in seconds:

time = timeD - time0

Divide by the number of seconds in the day, and round the resulting value:

days = time/86400

days = int(#{days})

Find out how many seconds are left of the incomplete day:

c = days*86400

d = time - c

Calculate how much it is in the clock and round:

e = d/3600 hours = int(e)

Figure out how many seconds are left of the incomplete hours:

g = hours3600

h = d - g

Translate it in minutes and round it up:

i = h/60

minutes = int(i)

Figure out how many seconds we have left:

k = minutes60

seconds = h - k

And enter the time in the Message Text box:

The start is #{days} days #{hours} hours #{minutes} minutes and #{seconds} seconds

Code to insert into the calculator:

timestampD = convert_datetime("#{date_D} #{time_D}", "%d.%m.%Y %H:%M", "%s")
timestamp0 = #{timestamp}
timeD = int(#{timestampD})
time0 = int(#{timestamp0})
time = timeD - time0
days = time/86400
days = int(#{days})
c = days*86400
d = time - c
e = d/3600
hours = int(e)
g = hours*3600 
h = d - g
i = h/60 
minutes = int(i)
k = minutes*60 
seconds = h - k

The calculator also supports subtraction of date and time without typing.

For example:

'11.12.2021' - '8.12.2021) will return 3 (days)

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

How to Know the Next Birthday

In order that the bot could congratulate the person can use the function

birthdate(date) - it returns the nearest birthday. At the entrance takes the date of birth of the person. For example: reminder = birthdate('28.04.1994')

How to get the date of the day of the week

If you have regular weekly newsletters, you can get the day of the week to schedule a mailing.

weekday_date(weekday, b) - returns the date of the nearest day of the week. Accepts two parameters. The first parameter is the day of the week, the date of which must be determined (from 1 to 7). The second parameter indicates whether to return today’s date if the day you want is today. The second option is not required, by default False. Let’s take an example of its value:

Let’s say you configure your mailing list to arrive on Thursday. But what if the client just checked in on Thursday? Send him a message this Thursday or the next?

The second False parameter means that the message will be sent only next Thursday.

The second True parameter means that it will be sent on the same Thursday when the user has just registered.

In our example, the user registers on Thursday 13.02.2020. And we want to send a message to him next Thursday. So the return value is "20.02.2020".

For example, weekday_date(4) returns '20.02.2020'.

If the current day suits you only up to a certain time, you can use such a design: weekday_date(4, current_time < '13:00'), where you can replace the condition with the one you want.

How to get the date of the day of the month

If you have periodical activities on a certain day of the month, you can get a date for the next day of the month to schedule a mailing.

month_date(weekday, b) - returns the date of the nearest day of the month. Accepts two parameters. The first parameter is the day of the month, the date of which must be determined (from 1 to 31). The second parameter indicates whether to return today’s date if the day you want is today. The second option is not required, by default False. If you transfer the 31, and in the current month 30 days, you return the 30.

How to check for hits during working hours or non-working hours

Suppose the company works from the morning 9 to 6 pm and does not work on Saturday and Sunday.

Everything is recommended to be done step by step. First, let’s put a check in time:

current_time >= '9:00' AND current_time <= '18:00'

By analogy NOT working time:

current_time < '9:00' OR current_time > '18:00'

Next, let’s add a check if we did not write on the weekend, for this we will need weekday:

weekday != 6 AND weekday != 7

Join together

The condition that wrote during working hours:

current_time >= '9:00' AND current_time <= '18:00' AND weekday != 6 AND weekday != 7

The opposite condition is that wrote NOT during business hours:

current_time < '9:00' OR current_time > '18:00' OR weekday == 6 OR weekday == 7

Example

How to Convert Date and Time

The time date format is different everywhere and perhaps one of the integrated resources will return the time in a format that does not understand salebot. You’ll need to convert it.

convert_datetime(date, fin, f_out) - for date and time conversion 3 parameters: date string, input string format, result format string.

Example:

convert_datetime("2011-11-03", "%Y-%m-%d", "%Y/%m/%d") output will be 2011/11/03

The function of getting the current time in the specified get_datetime format. 1 parameter: in which format to return the time. Example:

get_datetime("%A") output will be Wednesday

Format string parameters description:

DirectiveValueExample

%a

Day of the week reduced

Sun,Mon,...

%A

Full named day of the week

Sunday,Monday,....,Saturday

%w

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

0, 1, …, 6

%d

Day of the month as a decimal number with zeros

01, 02, …, 31

%b

Month as an acronym

Jan,Feb,....,Dec.

%B

Month’s full name

January,February,....,December

%m

A month in the form of a decimal number with zeros

01, 02, …, 12

%y

Year as a double digit number

00, 01, …, 99

%Y

Four digit value of the year

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

%H

Hour (24-hour format) double digit

00, 01, …, 23

%I

Hour (12-hour format) double digit

01, 02, …, 12

%p

Equivalent to AM or PM

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

%M

Minutes in Double Digit Format

00, 01, …, 59

%S

Seconds in Double Digit Format

00, 01, …, 59

%f

Microseconds per six-pointer

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

Triple value of the day of the year

001, 002, …, 366

%U

The week number in a year (Sunday as the first day of the week) is a decimal number supplemented with zeros. All days of the New Year preceding the first Sunday are considered to be the zero week.

00, 01, …, 53

%W

The week number in a year (Monday as the first day of the week) is a decimal number. All days of the new year preceding the first Monday are considered to be the zero week.

00, 01, …, 53

%%

%symbol

%

%s

timestamp

1607926200

All other symbols represent themselves.

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

Last updated