QuickSnip LogoA light blue logo with a thunderbolt in bracketsQuickSnip

Get Day of the Year

Calculates the day of the year (1-365 or 1-366 for leap years) for a given date.

  • date
  • day-of-year

Install

npx shadcn@latest add https://quicksnip.dev/r/get-day-of-the-year.json

This snippet is not verified yet. Review it before use, or help verify it.

Code

const getDayOfYear = (date) => {
  const startOfYear = new Date(date.getFullYear(), 0, 0);
  const diff = date - startOfYear + (startOfYear.getTimezoneOffset() - date.getTimezoneOffset()) * 60 * 1000;
  return Math.floor(diff / (1000 * 60 * 60 * 24));
};

Usage

getDayOfYear(new Date('2024-12-31')) // Returns: 366 (Leap year)