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

283
Vistas
How to validate From vs To dates using moment.js?

I am using Nestjs Crud to build certain CRUD endpoints. In one of the PATCH/POST endpoints, I am trying to validate dates that I am getting from the Frontend (validFrom, ValidTo).

Conditions:

  1. ValidFrom date should be greater than or equal to today's date.
  2. ValidTo date should be in the future (not today ofcourse)
  3. ValidTo should be greater than validFrom.

This is the code that is there:

public async validateDates(validFrom: string, validTo: string): Promise<void> {
    if (moment(validTo).isSameOrBefore(moment().startOf('day'))) {
      throw new BadRequestException('validTo date should be should be greater than todays date.');
    } else if (moment(validFrom).isBefore(moment().startOf('day'))) {
      throw new BadRequestException('validFrom date should not be in the past.');
    } else if (moment(validFrom).isSameOrAfter(moment(validTo))) {
      throw new BadRequestException('validFrom date should be less than validTo date.');
    }
  }

IF I try to enter validTo date as today's date, I get the error 'validFrom date should be less than validTo date', but ideally, I should be getting the error ''validTo date should be should be greater than todays date.'

Is there a better way to perform these validations?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

What do you pass into the validFrom and validTo? If it contains time then you would need to do something like moment(validTo).startOf('day').isSameOrBefore(moment().startOf('day'))

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