JS根据指定日期获取该日期所在的周一和周日

/**
 * @param 日期(yyyy-mm-dd)
 * @author QC
 * @date 2018-03-15
 * */
getMonDayAndSunDay(datevalue) {
    let dateValue = datevalue;
    let arr = dateValue.split("-");
    //月份-1 因为月份从0开始 构造一个Date对象
    let date = new Date(arr[0], arr[1] - 1, arr[2]);
    let dateOfWeek = date.getDay();//返回当前日期的在当前周的某一天(0~6--周日到周一)
    let dateOfWeekInt = parseInt(dateOfWeek, 10);//转换为整型
    if (dateOfWeekInt == 0) {//如果是周日
        dateOfWeekInt = 7;
    }
    let aa = 7 - dateOfWeekInt;//当前于周末相差的天数
    let temp2 = parseInt(arr[2], 10);//按10进制转换,以免遇到08和09的时候转换成0
    let sunDay = temp2 + aa;//当前日期的周日的日期
    let monDay = sunDay - 6;//当前日期的周一的日期
    let startDate = new Date(arr[0], arr[1] - 1, monDay);
    let endDate = new Date(arr[0], arr[1] - 1, sunDay);
    let sm = parseInt(startDate.getMonth()) + 1;//月份+1 因为月份从0开始
    let em = parseInt(endDate.getMonth()) + 1;
    //  alert("星期一的日期:"+startDate.getFullYear()+"-"+sm+"-"+startDate.getDate());
    //  alert("星期日的日期:"+endDate.getFullYear()+"-"+em+"-"+endDate.getDate());
    let start = startDate.getFullYear() + "-" + sm + "-" + startDate.getDate();
    let end = endDate.getFullYear() + "-" + em + "-" + endDate.getDate();
    let result = [];
    result.push(start);
    result.push(end);

    return result;
},
getDate() {
    let dt = new Date()
    let start = new Date(dt.getFullYear(), dt.getMonth(), 1)
    let start_ = new Date(this.getMonDayAndSunDay(start.Format('yyyy-MM-dd'))[0])
    let end = new Date(dt.getFullYear(), dt.getMonth() + 1, 5)
    let end_ = new Date(this.getMonDayAndSunDay(end.Format('yyyy-MM-dd'))[1])
    this.changeMonth(start_, end_);
},

猜你喜欢

转载自blog.csdn.net/qq_35624642/article/details/79569430