Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

240
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda