Get the week of the year

PHOTO EMBED

Wed Nov 27 2024 20:37:53 GMT+0000 (Coordinated Universal Time)

Saved by @desiboli #javascript #typescript

const getWeekOfYear = (date: Date): number => {
  const startOfYear = new Date(date.getFullYear(), 0, 1);
  const days = Math.floor(
    (date.getTime() - startOfYear.getTime()) / (24 * 60 * 60 * 1000)
  );
  return Math.ceil((days + startOfYear.getDay() + 1) / 7);
};
content_copyCOPY

Get the week of the year! Returns a number. e.g. 2024, the week between 25/11 - 1/12 is week #48.