Add dayOfYear

This commit is contained in:
Angelos Chalaris
2018-09-29 13:22:20 +03:00
parent 6504d476e7
commit 82d59bb8de
2 changed files with 17 additions and 0 deletions

16
snippets/dayOfYear.md Normal file
View File

@ -0,0 +1,16 @@
### dayOfYear
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
```

View File

@ -44,6 +44,7 @@ CSVToArray:string,array,utility,intermediate
CSVToJSON:string,array,object,advanced CSVToJSON:string,array,object,advanced
currentURL:browser,url,beginner currentURL:browser,url,beginner
curry:function,recursion,intermediate curry:function,recursion,intermediate
dayOfYear:date,beginner
debounce:function,intermediate debounce:function,intermediate
decapitalize:string,array,intermediate decapitalize:string,array,intermediate
deepClone:object,recursion,intermediate deepClone:object,recursion,intermediate