js根据日期获取本周的开始日期和结束日期

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_31971935/article/details/52669612
//根据日期获取本周的开始日期和结束日期
var weekArray = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
var getDays = function() {
    var now = new Date;
    var day = now.getDay();
    var week = "7123456";
    var first = 0 - week.indexOf(day);
    var f = new Date;
    f.setDate(f.getDate() + first);
    var last = 6 - week.indexOf(day);
    var l = new Date;
    l.setDate(l.getDate() + last);
    var a = [
        f, l
    ];
    var formatD = formatDate(a[0]);
    var formatendD = formatDate(a[1]);
    var EndDate = [formatD, new Date(formatD).getDay(), weekArray[new Date(formatD).getDay()], formatendD, new Date(formatendD).getDay(), weekArray[new Date(formatendD).getDay()]];
    return EndDate;
}

function formatTen(num) {
    return num > 9 ? (num + "") : ("0" + num);
}

function formatDate(date) {
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var hour = date.getHours();
    var minute = date.getMinutes();
    var second = date.getSeconds();
    return year + "-" + formatTen(month) + "-" + formatTen(day);
}
展开阅读全文

没有更多推荐了,返回首页