const today = new Date();
document.write(`<h1>오늘 날짜</h1>`)
document.write(`<h1>${today}</h1>`)

// 한달 전 날짜
  const monthAgo = new Date(today);
  monthAgo.setMonth(today.getMonth() - 1);
document.write(`<h1>한달 전 날짜</h1>`)
document.write(`<h1>${monthAgo}</h1>`)

// 날짜와 날짜 사이의 모든 날짜 구하기
const getDatesStartToLast = (startDate, lastDate) => {
  const result = [];
  while (startDate <= lastDate) {
    result.push(startDate.toISOString().split('T')[0]);
    startDate.setDate(startDate.getDate() + 1);
  }
  return result;
};
document.write(`<h1>날짜와 날짜 사이의 모든 날짜 구하기</h1>`)
document.write(`<h1>${getDatesStartToLast(monthAgo, today)}</h1>`)

// YYYY-MM-DD 형식으로 포맷팅
const format = (today) => {
  const year = today.getFullYear(); //년도
  const month = ('0' + (1 + today.getMonth())).slice(-2); //  월
  const day = ('0' + today.getDate()).slice(-2); // 날짜

  return year + '-' + month + '-' + day;
}

monthAgo.setMonth(today.getMonth() - 1);

document.write(`<h1> 오늘 날짜</h1>`)
document.write(`<h1>${format(today)}</h1>`)
document.write(`<h1>한달 전 날짜</h1>`)
document.write(`<h1>${format(monthAgo)}</h1>`)

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.