码迷,mamicode.com
首页 > Web开发 > 详细

JS时间 获取 当天,昨日,本周,上周,本月,上月

时间:2017-09-08 19:31:45      阅读:629      评论:0      收藏:0      [点我收藏+]

标签:ext   settimer   seconds   cti   nbsp   输出   new   second   day   

调用

setTimeRange (2);

 

function  setTimeRange (v) {
  var fmt = ‘YYYY-MM-DD HH:mm‘;
  var now = new Date();
  var end = new Date();
  if(v == 0){

  } else if(v == 1){

  } else if(v == 2){
    //昨日
    now.setDate(now.getDate() - 1);
    end.setDate(end.getDate() - 1);
  } else if(v == 3){
    //本周
    var nowDayOfWeek = now.getDay() ; //今天本周的第几天 
    if(nowDayOfWeek == 0) {
      nowDayOfWeek = 6; //周日
    } else {
      nowDayOfWeek = nowDayOfWeek - 1;
    }
    now.setDate(now.getDate() - nowDayOfWeek);
    end.setDate(now.getDate() + 6);
  } else if(v == 4){
    //上周
    var nowDayOfWeek = now.getDay() ; //今天本周的第几天 
    if(nowDayOfWeek == 0) {
      nowDayOfWeek = 6; //周日
    } else {
      nowDayOfWeek = nowDayOfWeek - 1;
    }

    now.setDate(now.getDate() - nowDayOfWeek - 7);
    end.setDate(now.getDate() + 6);
  } else if(v == 5){
    //本月
    now.setDate(1);

    var nextMonth = end.getMonth() + 1;
    var nextMonthFirstDay=new Date(end.getFullYear(), nextMonth,1);
    var oneDay= 1000 * 60 * 60 * 24;
    end = new Date(nextMonthFirstDay-oneDay);
  } else if(v == 6){
    //上月
    var nextMonth = now.getMonth() - 1;
    now.setMonth(nextMonth);
    now.setDate(1);

    nextMonth = nextMonth + 1;
    var nextMonthFirstDay= new Date(end.getFullYear(),nextMonth, 1);
    var oneDay= 1000 * 60 * 60 * 24;
    end = new Date(nextMonthFirstDay-oneDay);
  }

  now.setHours(0);
  now.setMinutes(0);
  now.setSeconds(0);
  end.setHours(23);
  end.setMinutes(59);
  end.setSeconds(59);

  //我们在这里 输出一下
  console.log(now + ‘:‘ + end);
}

JS时间 获取 当天,昨日,本周,上周,本月,上月

标签:ext   settimer   seconds   cti   nbsp   输出   new   second   day   

原文地址:http://www.cnblogs.com/eason-d/p/7495853.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!