根据指定时间获取前一周的时间

console.log(getWeekByDate('2017-05-05'));
// ["2017-04-29", "2017-04-30", "2017-05-01", "2017-05-02", "2017-05-03", "2017-05-04", "2017-05-05"]

function getWeekByDate(date) {
  let dd = new Date(date);
  let week = [dealDate(dd)];

  for(let i = 0; i < 6; i++) {
    dd.setDate(dd.getDate() - 1);
    week.push(dealDate(dd));
  }
  return week.reverse();
}

function dealDate(date) {
  return date.getFullYear() + '-' + addZero(date.getMonth() + 1) + '-' + addZero(date.getDate());
}

function addZero(num) {
  return num < 10 ? '0' + num : num;
}
posted @ 2017-05-26 14:49  ning0_o  阅读(276)  评论(0编辑  收藏  举报