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

409
Visualizações
yup date validation - Start Date must not be same as end date

I am currently stuck on how to do validation with yup for same date.

Currently I am able to validate if endDate is not before startDate using :

schema = yup.object().shape({
  startDate: yup.date().min(new Date(),'Please choose future date'),
  endDate: yup
          .date()
          .min(
             yup.ref("startDate"),
             "End date has to be more than start date"
          ),
})

But it is not checking for same date. I am well aware of this thread : Date range validation - Start Date must not be same as End Date in jquense / yup , but it is not solved yet and using momentjs. My company is strictly using dayjs for this project.

I hope you can help me with solution using JS or dayjs.

Thanks !

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can use this:

schema = yup.object().shape({
  startDate: yup.date().min(new Date(),'Please choose future date'),    
  endDate: yup
              .date()    
              .when('startDate',
                            (startDate, schema) => {
                                if (startDate) {
                                const dayAfter = new Date(startDate.getTime() + 86400000);
                              
                                    return schema.min(dayAfter, 'End date has to be after than start date');
                                  }
                              
                                  return schema;
                                
                            }),
})
about 4 years ago · Juan Pablo Isaza Relatório

0

Here is a solution that worked for me.


const validationSchema = Yup.object({
  startDate: Yup.date().required(),
  endDate: Yup.date().test(
    "same_dates_test",
    "Start and end dates must not be equal.",
    function (value) {
      const { startDate } = this.parent;
      return value.getTime() !== startDate.getTime();
    }
  ),
});

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