js获取今天是当年的第几周

2022-06-15  本文已影响0人  程序并非猿
function curWeek(){
    const today = new Date();
    let firstDayOfYear = new Date(today.getFullYear(), 0, 1);
    const dayOfWeek = firstDayOfYear.getDay();
    let spendDay = 1;
    if (dayOfWeek != 0) {
      spendDay = 7 - dayOfWeek + 1;
    }
    firstDayOfYear = new Date(today.getFullYear(), 0, spendDay);
    const d = Math.ceil((today.valueOf() - firstDayOfYear.valueOf()) / 86400000);
    return Math.ceil(d / 7);
}
上一篇下一篇

猜你喜欢

热点阅读