How to get the week number of a date in JavaScript

Calculating week numbers is essential for building calendar applications, scheduling systems, and time-based analytics in JavaScript applications. As the creator of CoreUI, a widely used open-source UI library, I’ve implemented week number calculations in numerous date-related components over 25 years of development. From my expertise, the most reliable approach is calculating the days between the target date and the first day of the year, then dividing by 7. This method handles edge cases correctly and works consistently across different years.

Calculate week number by finding days elapsed since January 1st and dividing by 7.

const getWeekNumber = (date) => {
  const firstDay = new Date(date.getFullYear(), 0, 1)
  const days = Math.floor((date - firstDay) / (24 * 60 * 60 * 1000))
  return Math.ceil((days + firstDay.getDay() + 1) / 7)
}

Here the function creates a Date object for January 1st of the target year, calculates milliseconds difference between dates, converts to days, and accounts for the starting day of the week. The Math.ceil() ensures partial weeks are counted as complete weeks, following ISO week numbering standards.

Best Practice Note:

This is the same approach we use in CoreUI calendar components for accurate week calculations. For ISO 8601 week numbering (where week 1 contains January 4th), consider using the Intl.DateTimeFormat API or specialized libraries like date-fns for complex requirements.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author

Subscribe to our newsletter
Get early information about new products, product updates and blog posts.
How to disable a button in JavaScript
How to disable a button in JavaScript

Understanding and Resolving the “React Must Be in Scope When Using JSX
Understanding and Resolving the “React Must Be in Scope When Using JSX

JavaScript Template Literals: Complete Developer Guide
JavaScript Template Literals: Complete Developer Guide

How to Center a Button in CSS
How to Center a Button in CSS

Answers by CoreUI Core Team