How to get the current day of year in JavaScript

2 Answers

0 votes
function getDayOfYear(date = new Date()) {
    const dateMilliseconds = Date.UTC(
                        date.getFullYear(),
                        date.getMonth(),
                        date.getDate(),
                        );

    const YearMilliseconds = Date.UTC(date.getFullYear(), 0, 0);
    
    const days = (dateMilliseconds - YearMilliseconds) / 1000 / 60 / 60 / 24;
    
    return days;
}


console.log(getDayOfYear(new Date())); 





/*
run:

356

*/

 



answered Dec 22, 2022 by avibootz
0 votes
const date = new Date();

const dayOfYear = Math.floor((date - new Date(date.getFullYear(), 0, 0)) / (1000 * 60 * 60 * 24));

console.log(dayOfYear); 





/*
run:

356

*/

 



answered Dec 22, 2022 by avibootz
...