Date Parser

Template
Function
1
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.
Overview
This is some text inside of a div block.
by
This is some text inside of a div block.

Heading 1

Heading 2

Heading 3

Heading 4

Heading 5
Heading 6

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Block quote

Ordered list

  1. Item 1
  2. Item 2
  3. Item 3

Unordered list

  • Item A
  • Item B
  • Item C

Text link

Bold text

Emphasis

Superscript

Subscript

Heading
Based in
This is some text inside of a div block.
Heading

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.