如何使以下代码生成当前月份加上前12个月?

时间:2017-12-15 06:43:07

标签: javascript date

以下代码采用今年的当前月份并以日历方式格式化:

let curYear = 2017
let curMonth = 11
let result = []
let firstDayOfMonth = new Date(curYear, curMonth, 1)
let lastDayOfMonth = new Date(curYear, curMonth, getLastDayOfMonth(curMonth))

result.unshift(firstDayOfMonth)
for (let d = prevDate(firstDayOfMonth); d.getDay() !== 6; d = this.prevDate(d)) {
  result.unshift(d)
}
for (let d = nextDate(firstDayOfMonth); d <= lastDayOfMonth; d = nextDate(d)) {
  result.push(d)
}
for (let d = nextDate(lastDayOfMonth); d.getDay() !== 0; d = nextDate(d)) {
  result.push(d)
}

console.log(JSON.stringify(result))

function nextDate (d) {
  let result = new Date(d.valueOf())
  result.setDate(d.getDate() + 1)
  return result
}

function prevDate (d) {
  let result = new Date(d.valueOf())
  result.setDate(d.getDate() - 1)
  return result
}

function getLastDayOfMonth (m) {
  let result = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  if (leapYear) {
    result[1] = 29
  }
  return result[m]
}

function leapYear () {
  if (curYear % 100 === 0) {
    return curYear % 400 === 0
  } else {
    return curYear % 4 === 0
  }
}

输出:

  

[&#34; 2017-11-25T16:00:00.000Z&#34;&#34; 2017-11-26T16:00:00.000Z&#34;&#34; 2017-11-27T16: 00:00.000Z&#34;&#34; 2017-11-28T16:00:00.000Z&#34;&#34; 2017-11-29T16:00:00.000Z&#34;&#34; 2017- 11-30T16:00:00.000Z&#34;&#34; 2017-12-01T16:00:00.000Z&#34;&#34; 2017-12-02T16:00:00.000Z&#34;&# 34; 2017-12-03T16:00:00.000Z&#34;&#34; 2017-12-04T16:00:00.000Z&#34;&#34; 2017-12-05T16:00:00.000Z&#34 ;,&#34; 2017-12-06T16:00:00.000Z&#34;&#34; 2017-12-07T16:00:00.000Z&#34;&#34; 2017-12-08T16:00: 00.000Z&#34;&#34; 2017-12-09T16:00:00.000Z&#34;&#34; 2017-12-10T16:00:00.000Z&#34;&#34; 2017-12- 11T16:00:00.000Z&#34;&#34; 2017-12-12T16:00:00.000Z&#34;&#34; 2017-12-13T16:00:00.000Z&#34;&#34; 2017-12-14T16:00:00.000Z&#34;&#34; 2017-12-15T16:00:00.000Z&#34;&#34; 2017-12-16T16:00:00.000Z&#34 ;, &#34; 2017-12-17T16:00:00.000Z&#34;&#34; 2017-12-18T16:00:00.000Z&#34;&#34; 2017-12-19T16:00:00.000Z& #34;&#34; 2017-12-20T16:00:00.000Z&#34;&#34; 2017-12-21T16:00:00.000Z&#34;&#34; 2017-12-22T16: 00:00.000Z&#34;&#34; 2017-12-23T 16:00:00.000Z&#34;&#34; 2017-12-24T16:00:00.000Z&#34;&#34; 2017-12-25T16:00:00.000Z&#34;&#34; 2017-12-26T16:00:00.000Z&#34;&#34; 2017-12-27T16:00:00.000Z&#34;&#34; 2017-12-28T16:00:00.000Z&#34 ;, &#34; 2017-12-29T16:00:00.000Z&#34;&#34; 2017-12-30T16:00:00.000Z&#34;&#34; 2017-12-31T16:00:00.000Z& #34;&#34; 2018-01-01T16:00:00.000Z&#34;&#34; 2018-01-02T16:00:00.000Z&#34;&#34; 2018-01-03T16: 00:00.000Z&#34;&#34; 2018-01-04T16:00:00.000Z&#34;&#34; 2018-01-05T16:00:00.000Z&#34;]

如何修改此功能以使其返回当前月份加上前12个月?

例如(从右到左阅读):

(and so on) ... 2017-10-05, ... 2017-11-05, ... 2017-12-05, ... , 2018-01-05]

Codepen:https://codepen.io/alexcheninfo/pen/zpGMvz

注意:我也欢迎Moment.js / Luxon替代品

1 个答案:

答案 0 :(得分:1)

// use moment.js
let curDate = moment();    
let newDate = moment().add(12, 'month');
let dateValues = [];

while (newDate > curDate) {
   dateValues.push(curDate.format('YYYY-MM-DD'));
   curDate.add(1,'month');
}