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

84
Views
Check if a week is between two weeks

I want to return true if a week is between two weeks. I.e., the period is week 48 (2021) to week 3 (2022). I then want to return true if a week is in between.

Lesson week and year is what I'm checking is inbetween. Start week & year and end week & year is the period.

My code below doesn't work for the example (week 48 2021 -> week 3 2022)

I'd rather not convert to Date objects but if it's needed I will.

const isSamePeriod = (
  lessonWeek,
  lessonYear,
  startWeek,
  startYear,
  endWeek,
  endYear
) => {
  if (lessonWeek >= startWeek && lessonYear === startYear) {
    if (lessonWeek <= endWeek && lessonYear === endYear) {
      return true;
    }
  }
  return false;
};

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

0

You can concatenate the values as strings of year + week provided the week is zero padded, then compare, e.g.

const isSamePeriod = (
  lessonWeek,
  lessonYear,
  startWeek,
  startYear,
  endWeek,
  endYear
) => {
  let z = n => ('0'+n).slice(-2);
  let lesson = lessonYear + z(lessonWeek);
  let start =  startYear + z(startWeek);
  let end =  endYear + z(endWeek);
  return lesson >= start && lesson <= end;
};

// Examples
[[47,2021],
 [48,2021],
 [50,2021],
 [ 1,2022],
 [ 3,2022],
 [ 4,2022]
].forEach(
   arr => console.log(arr + ' ' + isSamePeriod(...arr, 48,2021, 3,2022))
 );

You can also convert the week, year values to Dates, e.g.

// Convert year, week to Date
// Assumes ISO week: week starts on Monday, first week
// starts on Monday before first Thursday of year
function yearWeekToDate(year, week) {
  // 4th is always in first week
  let d = new Date(year, 0, 4);
  // Shift to prior Monday if not Monday
  d.setDate(d.getDate() - d.getDay() + 1);
  // Add 7 days for each week, decrement week number
  // as index starts at 1
  // This could be included in the above adjustment
  d.setDate(d.getDate() + 7 * (week - 1))
  return d;
}

const isSamePeriod = (
  lessonWeek,
  lessonYear,
  startWeek,
  startYear,
  endWeek,
  endYear
) => {
  let lesson = yearWeekToDate(lessonYear, lessonWeek);
  return lesson >= yearWeekToDate(startYear, startWeek) &&
         lesson <= yearWeekToDate(endYear, endWeek)
};

// Examples
[[47,2021],
 [48,2021],
 [50,2021],
 [ 1,2022],
 [ 3,2022],
 [ 4,2022]
].forEach(
  arr => console.log(`${arr} ${isSamePeriod(...arr, 48,2021, 3,2022)}`)
);

about 4 years ago · Juan Pablo Isaza Report

0

The checks need to be separate, but you have the second one nested inside the first. Also, you only need to check the week if the years match; see comments:

const isSamePeriod = (
    lessonWeek,
    lessonYear,
    startWeek,
    startYear,
    endWeek,
    endYear
) => {
    return (
        // Year is valid
        (lessonYear >= startYear && lessonYear <= endYear) ||
        // Lesson is not in the start year or is late enough in the start year
        (lessonYear !== startYear || lessonWeek >= startWeek) ||
        // Lesson is not in the end year or is early enough in the end year
        (lessonYear !== endYear || lessonWeek <= endWeek)
    );
};
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!