Press "Enter" to skip to content

使用nodejs获取下一个周一

function getNextDayOfWeek(targetDayOfWeek, forceNext) {

    const now = new Date();
    let daysUntilNext = (targetDayOfWeek - now.getDay()) % 7 || 7; // 4 表示星期四的索引为4,0-6表示周日到周六的索引
    if(forceNext && daysUntilNext === 1){
        daysUntilNext = daysUntilNext+7;
    }
    const d = new Date(now.getFullYear(), now.getMonth(), now.getDate() + daysUntilNext);
  
    const year = d.getFullYear();
    const month = String(d.getMonth() + 1).padStart(2, '0');
    const day = String(d.getDate()).padStart(2, '0');
  
    return `${year}-${month}-${day}`;
  }
  
  // 传入目标周几的参数,0 表示周日,1 表示周一,以此类推
  const targetDayOfWeek = 1; // 周一
  const forceNext = true; // 如果恰好是今天,强制下一周
  const nextDayOfWeekDate = getNextDayOfWeek(targetDayOfWeek, forceNext);
    console.log(nextDayOfWeekDate);
发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注