How to validate date for a future appointment

Hi,
I want to build an appointment booking bot, for this how do I validate the date entered by the client. I want to check if the date entered is a future date.

Thank you

Hey Ganesh

You can check the app.prediction object it should have a date field in case the user has typed out a date in the message.

1 Like

How do you get the current date, to compare it with the date entered.

You can create a new date object from the date available inside app.prediction.

You can also create another date object which corresponds to current time.

Post that, you can compute the time difference which should be positive for future dates

The following snippet assumes you have a date object obtained with app.prediction.

new Date().getTime() - date.getTime() > 0

Date Object Reference

1 Like

This is the program I wrote to validate whether an entered date is a future date

return new Promise((resolve, reject) => {

// Check if entered date is a future date

app.log("Inside date validator");
let date = app.prediction.Date;


if (new Date().getTime() - date > 0){
    return resolve();
}

else{
    
    app.log("Inside Error");
    return resolve("Enter a future date");
}



// let date = app.prediction.date[0];
// //let date = new Date(pDate[0].value.value);
// //let dateStr = date.toLocaleDateString();

});

I got the following log message for a “past date”:

printing context message {}
4 minutes ago firsttimeusernotyellowmessenger -x1594876894265- 579129941863219706172663239
4 minutes ago printing context message {}
4 minutes ago {}
4 minutes ago firsttimeusernotyellowmessenger -x1594876894265- 579129941863219706172663239
4 minutes ago printing context message {}
4 minutes ago {}
4 minutes ago error
4 minutes ago firsttimeusernotyellowmessenger -x1594876894265- 579129941863219706172663239
4 minutes ago printing context message {}
4 minutes ago {}
4 minutes ago error
4 minutes ago firsttimeusernotyellowmessenger -x1594876894265- 579129941863219706172663239
4 minutes ago printing context message {}
4 minutes ago error
4 minutes ago Inside date validator
4 minutes ago Inside Error
4 minutes ago error

when you check the last 3 lines it means that the program is entering the else part, but then the control is not going back to the bot so that user may enter a valid date.
What am I doing wrong?

Thank You

In else you need to reject or resolve false so that the validator is not resolved and the user can’t proceed unless he enters the correct future date.
Please try the below code.
return resolve({
success : false,
customHandler:()=>{
return app.sendTextMessage(“Enter a future date”);
}})

3 Likes