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

249
Views
How do I find if an ISO date like "2021-12-12" happened in the last 3 days?

I am making a site that gets data from a speedrun.com API and puts the newest runs on the site in order of time. I recently added a feature where runs from the same day that the site is viewed will have a special icon showing that they are "NEW". This is my current solution (Javascript):

runs[i].date = "2021-12-12"; // The API does not return hours
if (new Date(new Date().getTime() - new Date().getTimezoneOffset() * 60000).toJSON().slice(0, 10) === runs[i].date) {
  alert("Today");
}

If the date was December 12th, 2021, this would alert Today.

This solution accounts for timezone differences and works well. However, I would like this to be possible for runs that happened in the last 3 days, so even if runs[i].date = "2021-12-11" (December 10th) or runs[i].date = "2021-12-11" (December 11th), it would still alert Today.

How would this be done?

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

0

Split it up into functions and compose them:

const DAY = 1000 * 60 * 60 * 24;

function datesAreInRange (date1, date2, days) {
  const ms = days * DAY;
  const diff = Math.abs(date2.getTime() - date1.getTime());
  return diff <= ms;
}

function getDateFromString (ymd) {
  const [y, m, d] = ymd.split('-').map(str => parseInt(str, 10));
  return new Date(y, m - 1, d);
}

const today = '2021-12-16';

const otherDates = [
  '2021-12-20',
  '2021-12-19',
  '2021-12-18',
  '2021-12-17',
  '2021-12-16',
  '2021-12-15',
  '2021-12-14',
  '2021-12-13',
  '2021-12-12',
];

for (const other of otherDates) {
  const inRange = datesAreInRange(...[today, other].map(getDateFromString), 3);
  console.log(`${other} is ${inRange ? '' : 'not '}in range.`)
}

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!