Files
30-seconds-of-code/snippets/dayOfYear.md
2020-09-15 21:52:00 +03:00

18 lines
543 B
Markdown

---
title: dayOfYear
tags: date,beginner
---
Gets the day of the year from a `Date` object.
- Use `new Date()` and `Date.prototype.getFullYear()` to get the first day of the year as a `Date` object, subtract it from the provided `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.
```js
const dayOfYear = date =>
Math.floor((date - new Date(date.getFullYear(), 0, 0)) / 1000 / 60 / 60 / 24);
```
```js
dayOfYear(new Date()); // 272
```