-
-
Save parzibyte/161904f01528c64303e8cb11897e538a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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