I am using Dialogflow to pass the dateofbirth and convert it into specific format using a particular function written in helperfile in webhook end .
For this,I need to validate whether dateofbirth in in correct format before converting it inside helper file.
dateofbirth format to be validated: mm/dd/yyyy and 20 August,1989 (dd Month,Year)
Only if the dateofbirth are in the above mentioned format,It can go inside function.
The code is given below:
const formatdob = (df,globalParameter,dob) => {
try {
let dob = df._request.queryResult.queryText //taking input from user
globalParameter["dob"]=dob; //assigning it to Dialogflow parameters
if(dob.length!=0 ){ //need to validate whether dob is in above mentioned format
const d = new Date(dob);
return new Date(
Date.UTC(
d.getFullYear(),
d.getMonth(),
d.getDate(),
d.getHours(),
d.getMinutes(),
d.getSeconds()
)
).toISOString();
}
console.log("else block");
return dob;
}
catch (err) {
console.log(err)
throw err;
}
};
module.exports = formatdob;
I tried using regex but its getting too complicated.Could you please help me out with this?
I used Moment.js for the above question as suggested in the comment section.
const moment=require("moment");
/**
* Webhook integration with bigquery to fetch user details
* @param {object}
*/
const formatdob = (df,globalParameter,dob) => { //birthDate should be in mm/dd/yyyy format
try {
console.log("globalParameter",globalParameter);
if(dob===""){
let dob = df._request.queryResult.queryText
globalParameter["dob"]=dob;
if(moment(dob, "MMMM DD,YYYY").isValid() || moment(dob, "MM/DD/YYYY").isValid()){
const d = new Date(dob);
return new Date(
Date.UTC(
d.getFullYear(),
d.getMonth(),
d.getDate(),
d.getHours(),
d.getMinutes(),
d.getSeconds()
)
).toISOString();
}
return dob;
}
console.log("else block");
return dob;
}
catch (err) {
console.log(err)
throw err;
}
};
module.exports = formatdob;