Skip to content

Instantly share code, notes, and snippets.

@theboyofdream
Last active August 25, 2023 13:45
Show Gist options
  • Save theboyofdream/712ca4fd6a5ccb015f9e4086256b6957 to your computer and use it in GitHub Desktop.
Save theboyofdream/712ca4fd6a5ccb015f9e4086256b6957 to your computer and use it in GitHub Desktop.
Find week number using vanillaJs.
/**
* 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