Date Parser

Template
Function
33
Template
Function
by
Zoran Slamkov

Convert input messages to human readable dates.

Created:

Heading

Voiceflow APIs used:

Channels
No items found.
Created By
Zoran Slamkov
This is some text inside of a div block.

Function Code Snippet

 
export default async function main(args) {
const { originalDate, timezone } = args.inputVars;
let timezoneOffset = timezone;

// Validate our input values
if (!originalDate) {
return {
outputVars: {
error: `No original date was passed`
},
next: {
path: 'error'
},
trace: [
{
type: 'debug',
payload: {
message: `No original date was passed`
}
}
],
}
}

if (timezoneOffset === undefined || isNaN(parseFloat(timezoneOffset))) { timezoneOffset = 0; }

try {
// Send the original date and timezone offset to the server, which will perform the date conversion
const result = await fetch("https://api-date.voiceflow.fr/date", {
method: 'POST',
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
date: originalDate,
tz: Number(timezoneOffset)
})
});

// SUCCESS - Output the parsed dates
const responseBody = result?.json;
if (responseBody.date) {
let date = new Date(responseBody.date);
return {
outputVars: {
ISODate: responseBody.date,
humanReadableDate: date.toLocaleString()
},
next: {
path: 'success'
},
trace: [
{
type: 'debug',
payload: {
message: `Converting string "${originalDate}" to ${responseBody.date}`
}
}
],
}
}

// FAILURE - The server gave an error response, so report that back to the designer
return {
outputVars: {
error: `Unable to convert the given date`
},
next: {
path: 'error'
},
trace: [
{
type: 'debug',
payload: {
message: `API response error while trying to convert the given date`
}
}
],
}
} catch (error) {
return {
outputVars: {
error
},
next: {
path: 'error'
},
trace: [
{
type: 'debug',
payload: {
message: error
}
}
],
}
}
}
copy-icon

Explore More Templates

Build and submit a Template to have it featured in the community.

ghraphic
No items found.
No items found.