ALABADAMA
ALABADAMA

Reputation: 121

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. 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;
}

Upvotes: 2

Views: 10285

Answers (3)

Rishabh Kumar
Rishabh Kumar

Reputation: 557

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.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Upvotes: 1

sid8491
sid8491

Reputation: 6800

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]

enter image description here

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.

Upvotes: 0

RobG
RobG

Reputation: 147413

According 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]}`);
});

Upvotes: 1

Related Questions