Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

89
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda