Week of year

JavaScript, Date · Aug 15, 2021

Returns the zero-indexed week of the year that a date corresponds to.

const weekOfYear = date => {
  const startOfYear = new Date(date.getFullYear(), 0, 1);
  startOfYear.setDate(startOfYear.getDate() + (startOfYear.getDay() % 7));
  return Math.round((date - startOfYear) / (7 * 24 * 3600 * 1000));
};

weekOfYear(new Date('2021-06-18')); // 23

More like this

  • Day of year

    Gets the day of the year (number in the range 1-366) from a Date object.

    JavaScript, Date · Oct 19, 2020

  • Quarter of year

    Returns the quarter and year to which the supplied date belongs to.

    JavaScript, Date · Oct 22, 2020

  • Number of days in month

    Gets the number of days in the given month of the specified year.

    JavaScript, Date · Jun 13, 2021