js或者月和周不能时间段的数据

获取今天日期格式

function now_day_scope() {
    var date = new Date();
    var today = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
    return [today + " 00:00:00", today + " 23:59:59"]
}
function com_to_day() {
    var date = new Date();
    var today = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
    return today
}

获取本周


function now_Week_scope() {
    var date = new Date();

    var week = new Date(date - (date.getDay() - 1) * 86400000)
    var firstMonth = (week.getMonth() + 1) < 10 ? "0" + (week.getMonth() + 1) : (week.getMonth() + 1)
    var weekFirstDays = (week.getDate() < 10 ? "0" + week.getDate() : week.getDate())
    var startDate = week.getFullYear() + '-' + firstMonth + '-' + weekFirstDays + " 00:00:00";

    var weekLastDay = new Date((week / 1000 + 6 * 86400) * 1000)
    var lastMonth = (weekLastDay.getMonth() + 1) < 10 ? "0" + (weekLastDay.getMonth() + 1) : (weekLastDay.getMonth() + 1)
    var weekLastDays = (weekLastDay.getDate() < 10 ? "0" + weekLastDay.getDate() : weekLastDay.getDate())
    var endDate = weekLastDay.getFullYear() + '-' + lastMonth + '-' + weekLastDays + " 23:59:59";
    return [startDate, endDate]
}

最近七天


function now_Week_scope_arr() {
    var dateArr = []
    for (var i = 0; i < 7; i++) {
        var now = new Date();
        var firstDay = new Date(now - (now.getDay() - 1) * 86400000);
        firstDay.setDate(firstDay.getDate() + i);
        mon = (Number(firstDay.getMonth()) + 1 ) <10 ? "0"+(Number(firstDay.getMonth()) + 1 ) :(Number(firstDay.getMonth()) + 1 ) ;
        var day = firstDay.getDate() < 10 ? "0"+firstDay.getDate():firstDay.getDate();
        dateArr.push(now.getFullYear() + "-" + mon + "-" +day)
    }
    return dateArr;
}

本月

function now_month_scope_arr() {
    //本月第一天
    var Nowdate = new Date();
    var MonthFirstDay = new Date(Nowdate.getYear(), Nowdate.getMonth(), 1).getDate();
    //本月最后一天 
    var Nowdate = new Date();
    var tmpDate = new Date(Nowdate.getYear(), Nowdate.getMonth() + 1, 1);
    var MonthLastDay = new Date(tmpDate - 86400000).getDate();

    var date = new Date();
    var arr = [];
    for (var i = MonthFirstDay; i <= MonthLastDay; i++) {
        var month = (date.getMonth() + 1) < 10 ? "0" +(date.getMonth() + 1) :(date.getMonth() + 1);
        var day = i < 10 ? "0"+i :i
        arr.push(date.getFullYear() + "-" + month + "-" + day)
    }
    return arr;
}

本月

function now_month_scope() {
    var date = new Date();
    var MonthFirstDay = new Date(date.getFullYear(), date.getMonth(), 1);
    var MonthNextFirstDay = new Date(date.getFullYear(), date.getMonth() + 1, 1);
    var MonthLastDay = new Date(MonthNextFirstDay - 86400000);
    // 本月第一天
    var year = MonthFirstDay.getFullYear()
    var month = (MonthFirstDay.getMonth() + 1) < 10 ? "0" + (MonthFirstDay.getMonth() + 1) : (MonthFirstDay.getMonth() + 1)
    var todayState = (MonthFirstDay.getDate() < 10 ? "0" + MonthFirstDay.getDate() : MonthFirstDay.getDate())
    var startDate = year + "-" + month + "-" + todayState + " 00:00:00"
    // 本月最后一天
    var todayEnd = (MonthLastDay.getDate() < 10 ? "0" + MonthLastDay.getDate() : MonthLastDay.getDate())
    var endDate = year + "-" + month + "-" + todayEnd + " 23:59:59"
    return [startDate, endDate]

}

上月


function last_month_scope() {
    var nowdays = new Date();
    var year = nowdays.getFullYear();
    var month = nowdays.getMonth();
    if (month == 0) {
        month = 12;
        year = year - 1;
    }
    if (month < 10) {
        month = '0' + month;
    }
    var myDate = new Date(year, month, 0);
    var startDate = year + '-' + month + '-01 00:00:00'; //上个月第一天
    var endDate = year + '-' + month + '-' + myDate.getDate() + ' 23:59:59';//上个月最后一天

    return [startDate, endDate]
}

本年

function now_year_scope() {
    var date = new Date();
    var year = date.getFullYear();
    return [year + "-01-01 00:00:00", year + "-12-31 23:59:59"]

}

你可能感兴趣的:(js,javascript,前端,vue.js)