Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created January 30, 2022 04:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/161904f01528c64303e8cb11897e538a to your computer and use it in GitHub Desktop.
Save parzibyte/161904f01528c64303e8cb11897e538a to your computer and use it in GitHub Desktop.
const numeroDeSemana = fecha => {
const DIA_EN_MILISEGUNDOS = 1000 * 60 * 60 * 24,
DIAS_QUE_TIENE_UNA_SEMANA = 7,
JUEVES = 4;
fecha = new Date(Date.UTC(fecha.getFullYear(), fecha.getMonth(), fecha.getDate()));
let diaDeLaSemana = fecha.getUTCDay(); // Domingo es 0, sábado es 6
if (diaDeLaSemana === 0) {
diaDeLaSemana = 7;
}
fecha.setUTCDate(fecha.getUTCDate() - diaDeLaSemana + JUEVES);
const inicioDelAño = new Date(Date.UTC(fecha.getUTCFullYear(), 0, 1));
const diferenciaDeFechasEnMilisegundos = fecha - inicioDelAño;
return Math.ceil(((diferenciaDeFechasEnMilisegundos / DIA_EN_MILISEGUNDOS) + 1) / DIAS_QUE_TIENE_UNA_SEMANA);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment