Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

281
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!