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

320
Vistas
How to get weeks between a date and weekofyear, year tuple

How do I get the number of weeks between a given date and (weekofyear, year) tuple? Preferably using momentjs.

function getWeeksBetweenStartDate(weekOfYear, year)
  const date = moment().set({
    year: 1982,
    month: 3,
    day: 18,
    hour: 0,
    minute: 0,
    second: 0,
  });
  const weeksSinceDate =
    moment()
      .year(year)
      .isoWeek(weekOfYear)
      .day(0)
      .hour(0)
      .minute(0)
      .subtract(date)
      .weeks() ;
}

function getWeeks(date) {
  return getWeeksBetweenStartDate(date.isoWeeks(), date.year());
}

  getWeeks(
    moment().set({
      year: 1982,
      month: 3,
      day: 21,
      hour: 0,
    })
  )  // returns 0

  getWeeks(
    moment().set({
      year: 1982,
      month: 3,
      day: 23,
      hour: 0,
      minute: 0,
    })
  ) // returns 1

Even though the two example dates are part of same week, a different week delta is returned.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

There are several mistakes in the code I think:

  • First, you should not do that .subtract(date) in getWeeksBetweenStartDate, after subtraction, the date will become nearby 1970 (The UTC epoch). Instead, you should
  • You should not use the isoWeeks method, because in isoWeeks, Monday is the beginning of a week.
  • Had Better set moment.locale('us') at the beginning, to ensure a week always begins at Sunday.

The following maybe the correct code you expect:


moment.locale("us")
 
function getWeeksBetweenStartDate(date) {
  const weeksBegin = moment().set({
    year: 1982,
    month: 3,
    day: 18,
    hour: 0,
    minute: 0,
    second: 0,
  }).weeks();
  const weeksEnd = date.weeks();
  return weeksEnd - weeksBegin;
}

function getWeeks(date) {
  return getWeeksBetweenStartDate(date);
}

console.log(getWeeks(
    moment().set({
      year: 1982,
      month: 3,
      day: 21,
      hour: 0,
    })
    )  // returns 0
    ,
    getWeeks(
    moment().set({
      year: 1982,
      month: 3,
      day: 23,
      hour: 0,
      minute: 0,
    })
    ) // returns 1
)
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