js获取当前日期是一年中的第几天

js获取当前日期为一年中的第几天

const currentYear = new Date().getFullYear().toString();
// 今天减今年的第一天(xxxx年01月01日)
const hasTimestamp = new Date() - new Date(currentYear);
// 86400000 = 24 * 60 * 60 * 1000
const hasDays = Math.ceil(hasTimestamp / 86400000) + 1;
console.log('今天是%s年中的第%s天', currentYear, hasDays);
// 结果:今天是2018年的第329天

猜你喜欢

转载自www.cnblogs.com/baimeishaoxia/p/11806438.html