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

268
Views
Comparing Dates in JavaScript For any Give Date Format

Suppose I have different formats of Dates like "MM/DD/YY", "DD/MM/YY", "YY/MM/DD", I want to compare dates based on any of the above given formats, in JavaScript. Something Like:

let date1 = "21/04/2021";
let date2 = "25/01/2021";
let format = "DD/MM/YY";
Date(date1, format) > Date(date2, format);
True
Date(date1, format) < Date(date2, format);
False
Date(date1, format) === Date(date2, format);
False

Also both dates will have same format while comparing. Is there a better way to do it other then using split('/')?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

After trying multiple solutions I found best way to compare dates for any given format using moment library.

let result = moment(date1, format).isBefore(moment(date2, format))? -1: 1;

  1. result = -1 means date1 is smaller
  2. result = 1 means date1 is greater or equal
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!