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()); 
}
posted @ 2020-02-06 19:58  醉梦者  阅读(1109)  评论(0编辑  收藏  举报