其他分享
首页 > 其他分享> > js获取上周、上个月时间开始时间和结束时间

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)

标签:js,Date,nowdate,时间,上个月,var,new,getMonth,getDate
来源: https://www.cnblogs.com/xhrr/p/11991946.html