【问题标题】:Change date format in dialogflow在对话流中更改日期格式
【发布时间】:2020-12-20 12:18:01
【问题描述】:

我目前正在尝试使用对话流构建聊天机器人/代理,但老实说,我对编程业务/IT 方面的任何事情一无所知。我是一名学生,曾参加过客座讲座,向我们展示了如何创建聊天机器人哈哈。但我很感兴趣,坐下来尝试为我的作品创作一个。一个简单的机器人,可以告诉客户营业时间并提供一些信息以节省我们的一些电话。到现在为止还挺好。我想包含预订餐桌的功能,我的问题如下: 我已经阅读了许多有关更改日期和时间格式以接收“周四下午 4 点”而不是“2020-12-26T16:00:00+01:00”的格式的问题。 所以正如我所说,到目前为止我不知道如何更改代码以获得不同的输出,所以我的问题是你是否可以告诉我我必须在哪里执行此操作,或者我可以在哪里找到解决方案。不要误会我的意思,我很想知道该怎么做,所以是的,如果你能保存那个圣诞礼物,我会很高兴的:) 最好的祝福 莫

【问题讨论】:

    标签: dialogflow-es dialogflow-es-fulfillment


    【解决方案1】:

    所以,您的问题含糊不清,缺乏细节。
    如果您想在当地时间将“2020-12-26T16:00:00+01:00”转换为“星期四下午 4 点”,这里有一些辅助函数可以实现:

    function convertParametersDateTime(date, time){
      return new Date(Date.parse(date.split('T')[0] + 'T' + time.split('T')[1].split('+')[0]));
    }
    
    
    // A helper function that adds the integer value of 'hoursToAdd' to the Date instance 'dateObj' and return a new Data instance.
    function addHours(dateObj, hoursToAdd){
        return new Date(new Date(dateObj).setHours(dateObj.getHours() + hoursToAdd));
    }
    
    // A helper funciton that converts the Date instance 'dateObj' into a string that represents this time in English.
    function getLocaleTimeString(dateObj){
        return dateObj.toLocaleTimeString('en-US', {hour: 'numeric', hour12: true});
    }
    
    // A helper dunction that converts the Date instance 'dateObj' into a string that represents this date in English
    function getLocaleDateString(dateObj){
        return dateObj.toLocaleDateString('en-US', {weekday: 'long', month: 'long', day: 'numeric'});
    }
    

    这些是辅助函数。您必须根据您的意图在 Fulfillment 函数中调用它们。这是一个非常简单的例子:

    function makeAppointment (agent) {
        // Use the Dialogflow's date and time parameters to create Javascript Date instances, 'dateTimeStart' and 'dateTimeEnd',
        // which are used to specify the appointment's time.
        const dateTimeStart = convertParametersDateTime(agent.parameters.date, agent.parameters.time);
        const dateTimeEnd = addHours(dateTimeStart, appointmentDuration);
        const appointmentTimeString = getLocaleTimeString(dateTimeStart);
        const appointmentDateString = getLocaleDateString(dateTimeStart);
    
        agent.add(`Here's the summary of your reservation:\nDate&Time: ${appointmentDateString} at ${appointmentTimeString}`);
    
    }
    

    代码可能包含一些语法错误。这些功能提供了您正在寻找的东西,但您必须根据需要进行调整。

    【讨论】:

    • 好的。非常感谢您的详细回答,我会尽力而为:)
    猜你喜欢
    • 2010-10-12
    • 2013-06-03
    • 2015-03-03
    • 2011-11-18
    • 2022-01-28
    • 2014-10-23
    • 2016-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多