- 使用
Date
构造函数并Date.prototype.getFullYear()
获取一年中的第一天作为Date
对象。 date
从每天的毫秒数中减去一年中的第一天,得到结果。- 用于
Math.floor()
将生成的天数适当地四舍五入为整数。
const dayOfYear = date =>
Math.floor((date - new Date(date.getFullYear(), 0, 0)) / 1000 / 60 / 60 / 24);
dayOfYear(new Date()); // 272