you-dont-need / You-Dont-Need-Momentjs

List of functions which you can use to replace moment.js + ESLint Plugin
MIT License
13.27k stars 318 forks source link

Day of Year breaks due to Daylight savings #101

Open tainegilliam opened 3 years ago

tainegilliam commented 3 years ago

Your Day Of Year Native code will fail between midnight and 1am during daylight savings.

My replacement was:

Math.floor( (new Date().setHours(2) - new Date(new Date().getFullYear(), 0, 0)) / 1000 / 60 / 60 / 24 );

which forces the time for the day.

Alternatively:

Math.floor( (Date.UTC(year, month, day) - Date.UTC(year, 0, 0)) / 1000 / 60 / 60 / 24 )

The opposite code is quite simple, too:

new Date(year, 0 , dayOfYear)