LoginSignup
0
0

More than 3 years have passed since last update.

Moment.jsで特定の月のすべての日の配列を生成

Posted at
// 対象の年月(ここでは2020年8月)
const yearMonth = moment([2020, 7])

// 結果配列
const result = []

const startDayOfMonth = yearMonth.startOf('month').get('date')
const endDayOfMonth = yearMonth.endOf('month').get('date')
for (let d = start; d <= end; d++) {
  const m = moment([yearMonth.get('year'), yearMonth.get('month'), d])
  result.push(m)
}

console.log(result)

//  ["2020-08-01", "2020-08-02", "2020-08-03", "2020-08-04", "2020-08-05", "2020-08-06", "2020-08-07", "2020-08-08", "2020-08-09", "2020-08-10", "2020-08-11", "2020-08-12", "2020-08-13", "2020-08-14", "2020-08-15", "2020-08-16", "2020-08-17", "2020-08-18", "2020-08-19", "2020-08-20", "2020-08-21", "2020-08-22", "2020-08-23", "2020-08-24", "2020-08-25", "2020-08-26", "2020-08-27", "2020-08-28", "2020-08-29", "2020-08-30", "2020-08-31"]

参考

https://momentjs.com/docs/#/parsing/array/
https://momentjs.com/docs/#/manipulating/start-of/

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0