js获取上周、上个月时间开始时间和结束时间

 //获取系统当前时间  
    var nowdate = new Date();
    var y = nowdate.getFullYear();
    var m = nowdate.getMonth()+1;
    var d = nowdate.getDate();
    var formatnowdate = y+'-'+m+'-'+d;
    console.log(formatnowdate)
    //获取过去一周的时间
var myDate = new Date();
var weekDate = new Date(myDate.getTime() - 7 * 24 * 3600 * 1000);// 计算开始时间用
var weekDate2 = new Date(myDate.getTime() - 7 * 24 * 3600 * 1000);// 计算结束时间用

var day = weekDate.getDay();
var time = weekDate.getDate()-day+(day===0?-6:1);

var startDate = new Date(weekDate.setDate(time));
var beginTime = startDate.getFullYear()+'-'+(startDate.getMonth()+1)+'-'+startDate.getDate();
var endDate = new Date(weekDate2.setDate(time+6));
var endTime = endDate.getFullYear()+'-'+(endDate.getMonth()+1)+'-'+endDate.getDate();

    //获取系统前一个月的时间
    nowdate.setMonth(nowdate.getMonth()-1);
    var y = nowdate.getFullYear();
    var m = nowdate.getMonth()+1;
    var d = nowdate.getDate();
    var formatwdate = y+'-'+m+'-'+d;
    console.log(formatwdate)
posted @ 2020-06-16 16:49  番茄西红柿u  阅读(4491)  评论(0编辑  收藏  举报