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

266
Views
How to compare javascript dates (strings) with a specific format

I have two dates in a specific format (strings). I need to verify if the current date is lower than the max allowed date:

var date_current = '03_25_2022';
var date_max = '03_30_2022';

The format will always be m_d_Y. Since these are technically strings, what would be the best way to compare them as dates?

I'm using this function but I'm not sure of the approach:

function compareDates(d1, d2){
    var parts = d1.split('_');
    var d1 = Number(parts[1] + parts[2] + parts[0]);
    parts = d2.split('_');
    var d2 = Number(parts[1] + parts[2] + parts[0]);
    return d1 <= d2;
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can first convert these string into date object and then compare their timestamp as follow:

function strToDate(str) {
  const splits = str.split('_');
  if (splits.length !== 3) {
    throw Error("Invalid date");
  }
  return new Date(splits[2], parseInt(splits[0]) - 1, splits[1]);
}

let dateCurrent = strToDate('03_25_2022');
let dateMax = strToDate('03_30_2022');

console.log(dateMax.getTime() > dateCurrent.getTime())

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!