Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

239
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda