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. Right now, when I key in 4pm, it will print out at 16. My date it working perfectly, but the time is not. 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." But with the codes now, it prints out " 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;
}
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. Right now, when I key in 4pm, it will print out at 16. My date it working perfectly, but the time is not. 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." But with the codes now, it prints out " 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;
}
Share
edited Nov 23, 2018 at 2:36
ALABADAMA
asked Nov 23, 2018 at 2:06
ALABADAMAALABADAMA
1211 gold badge2 silver badges11 bronze badges
6
-
it would be easier to explain if you provided the format of
date
andtime
. What.split()
does is divide a string into array elements using the parameter to determine where to split. So a string like"XXXX-XX-XXTYY:YY:YY:YY"
split with.split('T')
would result in an array of["XXXX-XX-XX", "YY:YY:YY:YY"]
. – Randy Casburn Commented Nov 23, 2018 at 2:14 - @RandyCasburn Hi, I've edited my code above, the format is stated as above. – ALABADAMA Commented Nov 23, 2018 at 2:28
-
Date.parse
is redundant innew Date(Date.parse(date.split('T')[0]))
, you can usenew Date(date.split('T')[0])
. Anyway, it seems it should benew Date(date + 'T' + time)
. – RobG Commented Nov 23, 2018 at 2:34 - @RandyCasburn thanks, but it still prints as the 24hour format. – ALABADAMA Commented Nov 23, 2018 at 2:39
-
1
It's impossible to answer your question until you provide the format of the value returned by
agent.parameters.date
. The code infers ISO 8601 extended formatting, but the result indicates otherwise. – RobG Commented Nov 23, 2018 at 2:42
3 Answers
Reset to default 1According to the dialoflow documentation, sys.date returns a date string in ISO 8601 format like "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:
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. 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]}`);
});
This might be late however for someone looking to change date format via 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.
For more you can checkout: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
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.
When you say book table today 4.30pm, parameters extracted will be :
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'}
split()
function will split the string into multiple parts based on the delimiter you provide.
Below pic will help you understand what's happening with you function .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
Hope it helps.