CoreUI ONE — 250+ PRO components and 25+ admin templates for React, Angular, Vue, Bootstrap and Next.js, plus Data Grid, Scheduler, 4,300+ PRO icons and the Next.js boilerplate. One license. → See what you get

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. 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 set focus on an input field after rendering in React
How to set focus on an input field after rendering in React

Passing props to child components in React function components
Passing props to child components in React function components

How to loop inside React JSX
How to loop inside React JSX

How to validate an email address in JavaScript
How to validate an email address in JavaScript