其他分享
首页 > 其他分享> > js 根据n年第n周,获取此周的开始和结束日期

js 根据n年第n周,获取此周的开始和结束日期

作者:互联网

// 第n年的第n周,获取开始和结束时间 (星期一为一周的开始)
function week_date(weekstr){
    let year = weekstr.split("-")[0]
    let weekNo = weekstr.split("-")[1]
    // 此年1号是星期几
    let oneday = new Date(year+'-01-01').getDay() //0-6
    // 方便计算,当为星期天时为7
    if(oneday==0){
        oneday = 7
    }

    let one_fistday;
    let one_lastday;
    // 如果1号刚好是星期一
    if(oneday == 1){
         one_fistday = year+'-01-01'
         one_lastday = year+'-01-07'
    }else{
        let jj = 8-oneday
         one_fistday = (year-1)+'-12-'+(31-oneday+2>9?31-oneday+2:'0'+(31-oneday+2))
         one_lastday = year+'-01-'+(jj>9?jj:'0'+jj)
    }

    let fistday;
    let lastday;
    // 如果刚好是第一周
    if(weekNo ==1){
        fistday = one_fistday
        lastday = one_lastday
    }else{
        fistday = addDate(one_lastday,(weekNo-2)*7+1)
        lastday = addDate(one_lastday,(weekNo-1)*7)
    }
    return [fistday,lastday]
}

//日期加减法  date参数为计算开始的日期,days为需要加的天数   
//格式:addDate('2017-1-11',20) 
function addDate(date,days){ 
    var d=new Date(date); 
    d.setDate(d.getDate()+days); 
    var m=d.getMonth()+1; 
    return d.getFullYear()+'-'+(m>9?m:'0'+m)+'-'+(d.getDate()>9?d.getDate():'0'+d.getDate()); 
}

标签:01,此周,js,日期,let,year,lastday,fistday,oneday
来源: https://www.cnblogs.com/nogodie/p/12270114.html