Back to all functions

Date Parser

Convert input messages to human readable dates.

Created By
Zoran Slamkov
Voiceflow
download-icon
INPUT VARIABLES
{
originalDate
}
Date to convert to ISO 8601 and human readable date
{
timezone
}
timezone of the input date as a numeric offset
{
}
{
}
{
}
{
}
share-icon
OUTPUT VARIABLES
{
humanReadableDate
}
originalDate converted to human readable
{
ISODate
}
originalDate converted to an ISO 8601 format
{
error
}
error message (if any)
{
}
{
}
{
}
paths-icon
PATHS
{
success
}
success path
{
error
}
error path
{
}
{
}
{
}
{
}

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

Function Walkthrough

Explore More Functions

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

ghraphic