Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

238
Views
How to validate a date in a particular format in Javascript

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?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!