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

232
Views
Check if two dates are present in same week or not. (In JS)

E.g. Input 1:

date 1='2021-09-20'
date 2='2021-09-24'

Output 1:

Present in same week

Input 2:

date 1='2021-09-24'
date 2='2021-09-27'

Output 1:

Not in same week

Please provide solution in javascript.

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

0

const getWeek = (date) => {
  const janFirst = new Date(date.getFullYear(), 0, 1);
  // Source: https://stackoverflow.com/a/27125580/3307678
  return Math.ceil((((date.getTime() - janFirst.getTime()) / 86400000) + janFirst.getDay() + 1) / 7);
}

const isSameWeek = (dateA, dateB) => {
  return getWeek(dateA) === getWeek(dateB);
}

const dateA = new Date('2021-09-20')
const dateB = new Date('2021-09-24')
const dateC = new Date('2021-09-27')
console.log(isSameWeek(dateA, dateB));
console.log(isSameWeek(dateA, dateC));

A momentjs one-liner:

moment().format('W')
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!