Files
30-seconds-of-code/snippets/js/s/day-of-year.md
2023-05-07 16:07:29 +03:00

682 B

title, type, language, tags, cover, dateModified
title type language tags cover dateModified
Day of year snippet javascript
date
interior-3 2020-10-19T18:51:03+03:00

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