次のコードは、カレンダー形式でそれを今年の現在の月を取り、フォーマット:次のコードを作成すると、現在の月と前の12ヶ月が生成されます。
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
}
}
出力:
[ "2017-11-25T16:00:00.000Z"、 "2017 -11-26T16:00:00.000Z」、「2017-11-27T16:00:00.000Z」、「2017-11-28T16:00:00.000Z」、「2017-11-29T16:00:00.000Z」、 "2017-11-30T16:00:00.000Z"、 "2017-12-01T16:00:00.000Z"、 "2017-12-02T16:00:00.000Z"、 "2017-12-03T16:00:00.000Z "、" 2017-12-04T16:00:00.000Z "、" 2017-12-05T16:00:00.000Z "、" 2017-12-06T16:00:00.000Z "、" 2017-12-07T16:00: 「2017-12-08T16:00:00.000Z」、「2017-12-09T16:00:00.000Z」、「2017-12-10T16:00:00.000Z」、「2017-12-11T16:00.000Z」、 00:00.0 00Z "、" 2017-12-12T16:00:00.000Z "、" 2017-12-13T16:00:00.000Z "、" 2017-12-14T16:00:00.000Z "、" 2017-12-15T16:00 「00.000Z」、「2017-12-16T16:00:00.000Z」、「2017-12-17T16:00:00.000Z」、「2017-12-18T16:00:00.000Z」、「2017-12-19T16 「00:00.000Z」、「2017-12-20T16:00:00.000Z」、「2017-12-21T16:00:00.000Z」、「2017-12-22T16:00:00.000Z」、「2017-12 「2017-12-24T16:00:00Z」、「2017-12-25T16:00:00Z」、「2017-12-26T16:00:00Z」、「2017-12-25T16: 「2017-12-29T16:00:00.000Z」、「2017-12-28T16:00:00.000Z」、「2017-12-29T16:00:00.000Z」、「2017-12-30T16:00:00.000Z」、 "2017-12-31T16:00:00.000Z"、 "2018-01-01T16:00:00.000Z"、 "2018-01-02T16:00:00.000Z"、 "2018-01-03T16:00:00.000Z 」、 "2018-01-04T16:00:00.000Z"、 "2018-01-05T16:00:00.000Z"]、それは過去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代替
を歓迎し、あなたが使用して考えていより多くのサポートのためのライブラリのいくつかの種類? like moment.js –
あなたはすべてのコードを書きましたが、あなたは最も簡単な部分をどうやって行うのか分かりません。 – Adelin
出力はどのように表示されるべきですか? – gurvinder372