Working with dates and time in a bot

Working with time is possible only by the bot's time zone, the bot cannot determine the user's time zone.

To convert the current date, we can use helper:

{{formatDate (now) "DD.MM.YYYY" "en"}}

There is another helper to convert a user-input date:

{{formatDate Date1 "DD.MM.YYYY" "en"}}

Where Date1 is a user-input date value.

The bot can be implemented like this:

Use helper to add one day to the current date:

{{addDate (formatDate now "DD.MM.YYYY h:mm:ss a" "en") 1 "days" "DD.MM.YYYY h:mm:ss a" "en"}}

You can change 1 to the desired number of days that you add:

{{addDate (formatDate now "DD.MM.YYYY h:mm:ss a" "en") 15 "days" "DD.MM.YYYY h:mm:ss a" "en"}}

Change "days" to "months" to add months:

{{addDate (formatDate now "DD.MM.YYYY h:mm:ss a" "en") 1 "months" "DD.MM.YYYY h:mm:ss a" "en"}}

To "weeks" to add weeks:

{{addDate (formatDate now "DD.MM.YYYY h:mm:ss a" "en") 1 "weeks" "DD.MM.YYYY h:mm:ss a" "en"}}

To "years" to add years:

{{addDate (formatDate now "DD.MM.YYYY h:mm:ss a" "en") 1 "years" "DD.MM.YYYY h:mm:ss a" "en"}}

To "quarters" to add quarters:

{{addDate (formatDate now "DD.MM.YYYY h:mm:ss a" "en") 1 "quarters" "DD.MM.YYYY h:mm:ss a" "en"}}

"hours" to add hours:

{{addDate (formatDate now "DD.MM.YYYY HH:mm:ss a" "en") 2 "hours" "DD.MM.YYYY HH:mm:ss a" "en"}}

"minutes" to add minutes:

{{addDate (formatDate now "DD.MM.YYYY HH:mm:ss a" "en") 2 "minutes" "DD.MM.YYYY HH:mm:ss a" "en"}}

"seconds" to add seconds:

{{addDate (formatDate now "DD.MM.YYYY HH:mm:ss a" "en") 2 "seconds" "DD.MM.YYYY HH:mm:ss a" "en"}}

You can test these and other options in the sandbox

Helper for output month in a form of a word

The month in a date is usually output as a number, but if you need the name of the month, you can use helper:

{formatDate (now) "MMM" "en"}}

How to display the day of the week

The day of the week in the form of an ordinal number can be displayed using the helper:

{{formatDate now "d" "en"}}

In this format, days will be counted from 0 to 6, where 0 is Sunday, 1 is Monday, etc. To display the days of the week in abbreviated form ("Mo"), the helper will help:

{{formatDate now "dd" "en"}}

So you can display the days of the week in full ("Monday", "Tuesday", etc.):

{{formatDate now "dddd" "en"}}

The date will be converted depending on what day of the week the user logged in.

You can find information on other types of date format here

How to display the serial number of the week in the year

{{formatDate now 'w' 'en'}}

How to configure the bot's response depending on the user's current time

Get and convert the current time to required format

Get the current date and time with helper:

{{formatDate (now) "DD.MM.YYYY h:mm" "en"}}

Set a value to Time variable with Assign a variable component. Since we only want to know when user entered the bot, we need to remove the date from the helper and leave only hours and minutes:

{{formatDate (now) "h:mm:ss a" "en"}}

Process the time and translate to set screens

Put Fork component to the screen. Set The variable name to Time.

It’s easier to make the regular expression for the first interval — 10 to 19:

^1[0-8]\:[0-5][0-9]$

Add this regular expression to the Fork value. In Data Type select Regular expression. The default target is 19 to 10 interval.

Format the bot reply screens and we’re done.

((10|11|12):[0-9]{2}:[0-9]{2}\s(am)|([1-6]{1}):[0-9]{2}:[0-9]{2}\s(pm))

How to compare dates

Let’s make the users who wrote less than 15 days after the purchase and the ones who wrote after the 15-day period get messages with different texts.

This can be useful if the discount on the product is valid only 15 days after purchase or for other similar cases.

1. Ask the user when he made the last purchase.

2. Write the answer to Usday variable with the Input from the user. Select Date data type.

3. Convert today's date into seconds with the helper

{{now}}

4. Convert the date on which the last purchase was made into milliseconds using another helper:

{{formatDate Usday 'x' 'en'}}

Thus, we overwrite the variable Usday.

5. Subtract from today the date on which the last purchase was made:

{{subtract today Usday}}

Let's write the result of the subtraction to diff variable

6. Using gt comparison helper, determine whether more or less than 15 days passed:

{{#gt (toInt diff) 1296000000}}More than 15 days{{else}}Less than 15 days{{/gt}}

7. Check days variable with a fork and direct users to different screens depending on the time passed.

8. Design separate screens for those who wrote later than in 15 days and those who wrote earlier.