简体   繁体   English

Dialogflow 实现中的日期时间函数

[英]Date Time function in Dialogflow fulfillment

Can anyone explain what does these function means?谁能解释一下这些功能是什么意思?

setDate = agent.parameters.date.split('T')[0];
setTime = agent.parameters.time.split('T')[1].split(':')[0];

I am doing a booking for reservation and I want Google Assistant to print out the timing user enter as a 12-hour Format.我正在预订预订,我希望 Google 助理将用户输入的时间打印为 12 小时格式。 Right now, when I key in 4pm, it will print out at 16. My date it working perfectly, but the time is not.现在,当我在下午 4 点输入时,它会在 16 点打印出来。我的日期运行良好,但时间不是。 I've tried other methods, but I don't really understand that the "split" means.我尝试过其他方法,但我不太明白“拆分”的含义。

eg.例如。 If I say "book table today 4.30pm" , then google will reply as "You have booked on 2018-11-23 at 4:30PM."如果我说“book table today 4.30pm” ,那么谷歌会回复为“你已经在 2018-11-23 下午 4:30 预订了。” But with the codes now, it prints out " 2018-11-23 at 16"但是现在使用代码,它会打印出“ 2018-11-23 at 16”

This is my code:这是我的代码:

function makeBooking(agent){

bookingDate= agent.parameters.date.split('T')[0];
bookingTime = agent.parameters.time.split('T')[1].split(':')[0];

agent.add(`You have booked on ${availDate} at ${availTime}.`);

}

// A helper function that receives Dialogflow's 'date' and 'time' parameters and creates a Date instance.

function convertParametersDate(date, time){
  var resultDate = new Date(Date.parse(date.split('T')[0]));
  return resultDate;
}

According to the dialoflow documentation, sys.date returns a date string in ISO 8601 format like "2018-04-06T12:00:00-06:00".根据 dialoflow 文档, sys.date返回 ISO 8601 格式的日期字符串,如“2018-04-06T12:00:00-06:00”。

So if agent.parameters.date is a string in the same format, then in the makeBooking function, assuming the value is "2018-11-23T16:51:42+05:30" then:所以如果agent.parameters.date是相同格式的字符串,那么在makeBooking函数中,假设值为“2018-11-23T16:51:42+05:30”,那么:

function makeBooking(agent){ 
  // 2018-11-23
  var bookingDate= agent.parameters.date.split('T')[0];
  // 16
  var bookingTime = agent.parameters.time.split('T')[1].split(':')[0];
  // You have booked on 2018-11-23 at 16
  agent.add(`You have booked on ${availDate} at ${availTime}.`);
}

If you want the time to be "4:51 pm" instead, then you need to convert "16:51" to an appropriate format.如果您希望时间为“4:51 pm”,则需要将“16:51”转换为适当的格式。 There are many, many questions here already on reformatting date strings, in this case you want the date and time as separate strings, so you might use something like the following that returns an array of the date and time as separate elements:这里已经有很多关于重新格式化日期字符串的问题,在这种情况下,您希望将日期和时间作为单独的字符串,因此您可以使用类似以下内容的内容,将日期和时间的数组作为单独的元素返回:

 // "2018-11-23T16:51:42+05:30" function reformatDate(s) { // ["2018-11-23", "16:51:42+05:30"] var b = s.split('T'); // ["16", "51"] var t = b[1].slice(0,5).split(':'); return [b[0], `${t[0]%12||12}:${t[1]} ${t[0]<12?'am':'pm'}`]; } ["2018-11-23T16:51:42+05:30", "2018-11-23T06:16:42+05:30", "2018-11-23T00:01:42+05:30", "2018-11-23T23:55:42+05:30" ].forEach(s => { var parts = reformatDate(s); console.log(`You have booked on ${parts[0]} at ${parts[1]}`); });

Dialogflow's @sys.time parameter returns time in YYYY-MM-DDTHH:MM:SS+05:30 format where +05:30 is the time offset of India and it will vary from country to country. Dialogflow 的 @sys.time 参数以YYYY-MM-DDTHH:MM:SS+05:30格式返回时间,其中+05:30是印度的时间偏移量,它会因国家/地区而异。

When you say book table today 4.30pm , parameters extracted will be :当你说book table today 4.30pm 时,提取的参数将是:

parameters': { 'time': '2018-11-23T16:30:00+05:30' , 'date.original': 'today', 'time.original': '4:30 pm', 'date': '2018-11-23T12:00:00+05:30' }参数': { 'time': '2018-11-23T16:30:00+05:30' , 'date.original': '今天', 'time.original': '4:30 pm', 'date' :'2018-11-23T12:00:00+05:30' }

split() function will split the string into multiple parts based on the delimiter you provide. split()函数将根据您提供的分隔符将字符串拆分为多个部分。

Below pic will help you understand what's happening with you function .split('T')[1].split(':')[0]下面的图片将帮助您了解函数.split('T')[1].split(':')[0]

在此处输入图片说明

Below code should work better in your case:下面的代码应该在你的情况下工作得更好:

from datetime import datetime
setTime = agent.parameters.time.split('T')[1].split('+')[0]
setTime = datetime.strptime(setTime, '%H:%m:%S')
setTime = setTime.strftime('%I:%M %p')

this will give 04:30 PM这将给04:30 PM

Hope it helps.希望能帮助到你。

This might be late however for someone looking to change date format via fullfilment:然而,对于希望通过 fullfilment 更改日期格式的人来说,这可能为时已晚:

new Date('2020-05-01T12:00:00+05:30').toDateString()

Output: "Fri May 01 2020"

For finding hours and minutes, you can use getHours() and getMinutes() methods.要查找小时和分钟,您可以使用getHours()getMinutes()方法。

For more you can checkout: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date有关更多信息,您可以查看: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM