Slot filling
The easiest way to work with dates and time is using an Any text slot of the entity type Date.
Whenever a customer would say something like today, next Saturday or 12th of September the AI engine will transform this into a UTC date, relative to the timezone of the customer.
Date and time is relative
Flow works with builtin timezone support. If the current date and time of a customer in Amsterdam would be 2pm, 27th of August, it's translated to a UTC date time with the value 2019-08-27T12:00:00.000Z
With UTC it’s possible to transform a date to any local format. Since time is relative, tomorrow would mean something different for a customer in Japan, compared to someone living on the west coast of the US.
Formatting dates
If you want to format dates to a local format you need to use a code action.
To format dates and time we provide the popular Moment.js and Moment Timezone packages to be used within code actions.
// Convert a date to Dutch date format
moment(date).tz('Europe/Amsterdam').format('LLLL')
The following example code shows you how to format a data and add it as a param to be used inside your flows.
async payload => {
const {
params
} = payload
if(Array.isArray(params.some_date)) {
// Format a date in Europe/Amsterdam format
return {
params: {
...payload.params,
formatted_date: [{
value: moment(params.some_date[0].value).tz('Europe/Amsterdam').format('LLLL')
}]
}
}
}
}
Whenever the above code action is executed, it would convert a date and return it as a new param.
Using a user's timezone offset
Within the payload of a code action you'll receive any user profile data, including timezone offset (if available).
The offset is the number of hours a user relative to UTC. The example below shows how you can use the timezone offset to convert a date to a local format.
async payload => {
const {
params,
user
} = payload
const {
profile
} = user
if(!Array.isArray(params.some_date)) {
// Skip if there is no date to format
return
}
// Dynamic timezone based on user timezone offset
const region = profile.timezone < 0 ? `Etc/GMT${profile.timezone}` : `Etc/GMT+${profile.timezone || 0}`
return {
params: {
...payload.params,
formatted_date: [{
value: moment(params.some_date[0].value).tz(region).format('LLLL')
}]
}
}
}