Skip to main content

datetime extension

This extension provides template filters to convert input value to date/time. The filters throw an exception (BotError) if the conversion is not possible.

tz filter

Retrieve a time zone object from a string representation. An empty string is interpreted as local time.

Usage example: getting local time in Asia/Dubai timezone.

extensions:
datetime: {}
dialog:
- condition: true
response: |
{{ utc_time.astimezone("Asia/Dubai"|tz).isoformat() }}
🧑 hello
🤖 2023-05-29T20:28:40.864665+04:00

The set of supported time zones depends on your system. A well-known list of timezones can be found on Wikipedia (TZ identifier column).

If the timezone is not recognized, an error will occur:

extensions:
datetime: {}
dialog:
- condition: true
response: |
{{ "XYZ"|tz }}
🧑 hello
logs
✗ Unknown timezone: 'XYZ'

datetime filter

Convert input value to datetime. The input value can be of the following types: datetime, date, time, int, float, str.

In the following example, we convert the ISO 8601 format string into a datetime value and print string representing this date and time.

extensions:
datetime: {}
dialog:
- condition: true
response: |
{{ ("2023-05-24T10:00:00+00:00"|datetime).ctime() }}
🧑 hello
🤖 Wed May 24 10:00:00 2023

date filter

Convert input value to date. The input value can be of the following types: datetime, date, int, float, str

In the following example, we convert integer into a date value and print day of the week.

extensions:
datetime: {}
dialog:
- condition: true
response: |
{{ ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",
"Sunday"][(1684922400|date).weekday()] }}
🧑 hello
🤖 Wednesday

time filter

Convert input value to time. The input value can be of the following types: datetime, time, int, float, str

In the following example, we convert float into a time value and print time value in ISO 8601 format.

extensions:
datetime: {}
dialog:
- condition: true
response: |
{{ (1684922400.0|time).isoformat() }}
🧑 hello
🤖 10:00:00+00:00