programming-examples/js/Date/Write a JavaScript function to count the number of days passed since beginning of the year..js
2019-11-15 12:59:38 +01:00

8 lines
299 B
JavaScript

function days_passed(dt) {
var current = new Date(dt.getTime());
var previous = new Date(dt.getFullYear(), 0, 1);
return Math.ceil((current - previous + 1) / 86400000);
}
console.log(days_passed(new Date(2015, 0, 15)));
console.log(days_passed(new Date(2015, 11, 14)));