Day of year

JavaScript, Date · Oct 19, 2020

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

  • Use the Date constructor and Date.prototype.getFullYear() to get the first day of the year as a Date object.
  • Subtract the first day of the year from date and divide with the milliseconds in each day to get the result.
  • Use Math.floor() to appropriately round the resulting day count to an integer.
const dayOfYear = date =>
  Math.floor((date - new Date(date.getFullYear(), 0, 0)) / 1000 / 60 / 60 / 24);
dayOfYear(new Date()); // 272

More like this

  • Number of days in month

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

    JavaScript, Date · Jun 13, 2021

  • Day name

    Gets the name of the weekday from a Date object.

    JavaScript, Date · Nov 1, 2020

  • Add weekdays to date

    Calculates the date after adding the given number of business days.

    JavaScript, Date · Jan 7, 2021