js the current time-based, easy acquisition time (last 2 days, last week, last 2 weeks, last month, last 2 months, the past six months, the most recent year, week, month, year)

In the development of the company management background system encountered according to different time periods, such as "nearly a year, nearly half a year, nearly in March, nearly in January, nearly a week" to get and show different chart data needs, it is cumbersome, project development cycle is very short and he thought for a moment, although there are ideas, but short-lived fad about to write is quite time-consuming, so it is simply the Internet to find a ready-wheel it, but fortunately, the great God has written the same effect, but from my actual demand is still a little far away, now make reference to the god of code to do a note of it:

//格式化时间
function formatTime(param) {
  let y = param.getFullYear();
  let m = param.getMonth() + 1;
  let d = param.getDate();
  m = m < 10 ? ("0" + m) : m;
  d = d < 10 ? ("0" + d) : d;
  return y + "-" + m + "-" + d + " ";
}

/**
* 以当前时间为基础,便捷获取时间(最近2天,最近1周,最近2周,最近1月,最近2月,最近半年,最近一年,本周,本月,本年)
* @param { string } code
* @returns { Object }
*/
function getDate(code) {
  const date = new Date();
  let endTime = formatTime(date);
  let date1 = Date.parse(date);
  let start = '';
  let end = '';
  let oneDay = 1000 * 3600 * 24;

  switch (code) {
    //今天
    case 'today':
      start = new Date();
      break;
    //最近2天
    case 'lastTwoDay':
      start = date1 - oneDay * 2;
      break;
    //最近1周
    case 'lastOneWeek':
      start = date1 - oneDay * 7;
      break;
    //最近2周
    case 'lastTwoWeek':
      start = date1 - oneDay * 14;
      break;
    //最近1月
    case 'lastOneMonth':
      start = new Date();
      start.setMonth(start.getMonth() - 1)
      break;
    //最近2月
    case 'lastTwoMonth':
      start = new Date();
      start.setMonth(start.getMonth() - 2)
      break;
    //最近3月
    case 'lastThreeMonth':
      start = new Date();
      start.setMonth(start.getMonth() - 3)
      break;
    //最近半年
    case 'lastHalfYear':
      start = date1 - oneDay * 183;
      break;
    //最近一年
    case 'lastOneYear':
      start = new Date();
      start.setYear(start.getFullYear() - 1)
      break;
    //本周
    case 'thisWeek':
      let a = 6 - date.getDay();
      start = new Date(date1 - oneDay * a).setHours(0, 0, 0, 0);
      end = new Date(date1 + oneDay * (1 + date.getDay())).setHours(24, 0, 0, 0)
      break;
    //本月
    case 'thisMonth':
      start = new Date();
      start.setHours(0, 0, 0, 0)
      start.setMonth(start.getMonth(), 1)
      end = new Date(start)
      end.setHours(0, 0, 0, 0)
      end.setMonth(start.getMonth() + 1, 1)
      break;
    //本年
    case 'thisYear':
      start = new Date();
      start.setHours(0, 0, 0, 0)
      start.setMonth(0, 1)
      start.setYear(start.getFullYear())
      end = new Date(start)
      end.setHours(0, 0, 0, 0)
      end.setMonth(start.getMonth(), 1)
      end.setYear(start.getFullYear() + 1)
      break;
  }

  return {
    startTime: formatTime(new Date(start)),
    endTime: end ? formatTime(new Date(end)) : endTime,
  }
}

Very simple to use, output last week: console.log (getDate ( 'lastOneWeek')).

The effect of this method out of the last week: {startTime: "2019-09-22", endTime: "2019-09-29"}, such that only a process for the start and end dates of the interface, according to our actual needs, the need is data like this:

最近一周:["2019-09-22", "2019-09-23", "2019-09-24", "2019-09-25", "2019-09-26", "2019-09-27", "2019-09-28", "2019-09-29"]或者最近一月:["2019-08-29", "2019-08-30", "2019-08-31", "2019-09-01", ..., "2019-09-29"]。

Since this requirement is similar line graph showing an X-axis made with as:

Is not it tedious? Let them front-end processing, cumbersome, backstage colleagues write front-end interface to say that you do not deal with, too much trouble, we come back to this date you. Really conscience ah! ! !

Reference article: https://www.cnblogs.com/qinacao/p/9117514.html

Guess you like

Origin www.cnblogs.com/tnnyang/p/11607088.html