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

378
Vistas
Yup date validation, End date after start date

here's my code, i'm trying to force the two date fields always to be different, the end date must be at least 1 day after the start date.

const EditSchema = Yup.object().shape({

       StartDate: Yup.date()
      .transform(value => (isDate(value) ? undefined : value))
      .typeError('Enter a start date')
      .required('Enter a start date'),

       EndDate: Yup.date()
      .min(Yup.ref('StartDate'), 'End date must be after start date')
      .transform(value => (isDate(value) ? undefined : value))
      .typeError('Enter an end date')
      .required('Enter an end date')
});
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

After hours trying to figure out how to do it.

here's the code.

when you have a date picker and you select a date, it will be something like this "01/01/2021 00:00:00".

So if you need the end date to be at least 1 day after the start date, you've got to add 1 hour to the start date which will not let you put 2 equal dates.


   StartDate: Yup.date()
      .transform(value => (isDate(value) ? undefined : value))
      .typeError('Enter a date')
      .required('Enter a date')
      ),
      EndDate: Yup.date()
      .min(Yup.ref('StartDate'), 'End date must be after start date')
      .when('StartDate', (st, schema) => {
      
        if(st != null){
          st.setHours(st.getHours() + 1);
          return schema.min(st)

        //this else prevents your page from crashing if there's any other input
        
         }else
          return schema.min('1900-01-01')

      })
      .transform(value => (isDate(value) ? undefined : value))
      .typeError('Enter a date')
      .required('Enter a date')
  });
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