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

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

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 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