Last active
August 25, 2023 13:45
-
-
Save theboyofdream/712ca4fd6a5ccb015f9e4086256b6957 to your computer and use it in GitHub Desktop.
Find week number using vanillaJs.
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
/** | |
* Calculates the week number of a given date. | |
* @param {Date} date - The date for which to calculate the week number. | |
* @returns {number} - The week number of the provided date. | |
*/ | |
function getWeekNumber(date) { | |
const dd = date.getDate(); | |
const mm = date.getMonth(); | |
const yyyy = date.getFullYear(); | |
// Get the index of the first day of the month | |
const firstDayIndex = new Date(yyyy, mm, 1).getDay(); | |
// Calculate the week number | |
const weekNumber = Math.floor(firstDayIndex + dd / 7); | |
return weekNumber; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment