newDate()を使った日付の操作について

この記事を読むのにかかる時間 1未満

概要

コード

Dateを使って日付を取得する

const year = new Date(targetMonth.getFullYear()) //年を取得(2桁)

const year = new Date(targetMonth.getFullYear()) //年を取得

const month = new Date(targetMonth.getMonth()) //月を取得

const date = new Date(targetMonth.getDate()) //日を取得

const date = new Date(targetMonth.getDay()) //曜日を取得(0が日曜日)

Dateを使って使える構造に変更する

const lastDayOfMonth = new Date(targetMonth.getFullYear(), targetMonth.getMonth() + 1, 0).getDate();
 //new Date(年, 月, 0)は前月の最終日

const dateStr10th = `${check10th.getFullYear()}-${String(check10th.getMonth() + 1).padStart(2, '0')}-10`;
 // jsonデータなどと比較するため、"2026-01-10"などの形式の表記を作る。
 // String(...).padStart(2, '0'):01、02など、左に0をつける