月末日・月初日の取得
This content is not available in your language yet.
ここではオフィススクリプトを使って、月末日・月初日や月の日数を求める方法を、サンプルコードと併せて解説します。
月末日を取得する
Section titled “月末日を取得する”Dateオブジェクトを作成するときに、年・月・日を指定します。日に0を指定すると、指定した月の前月の末日になります。
つまり「翌月の 0 日」を指定すると、その月の末日が求められます。
function main(workbook: ExcelScript.Workbook) { /** 現在の日付 */ const now = new Date();
/** 今月の末日(翌月の 0 日) */ const endOfMonth = new Date(now.getFullYear(), now.getMonth() + 1, 0);
console.log(endOfMonth.toLocaleDateString()); // 2026/11/30(現在が 2026 年 11 月の場合)}うるう年かどうかも自動的に考慮されます。
// 月の値 2 は 3 月を表すため、3 月の 0 日 = 2 月の末日になるconsole.log(new Date(2024, 2, 0).toLocaleDateString()); // 2024/2/29console.log(new Date(2023, 2, 0).toLocaleDateString()); // 2023/2/280 日が前月の末日になる仕組み
Section titled “0 日が前月の末日になる仕組み”Dateオブジェクトは、存在しない日付が指定されると、自動的に繰り上げ(繰り下げ)を行います。
「1 日」の 1 日前である「0 日」は存在しないため、前月にさかのぼって前月の末日として扱われます。
以下は、3 月 1 日からsetDateで 1 日を引いた場合の例です。setDateに0が渡され、2 月 29 日になります。日付や加える数を変えて、繰り上げのしかたを確かめてみましょう。
date.setDate(date.getDate() - 1)
- Original date202431
- Date given by setDate(0)202430Does not exist
- Actual date2024229
- Day 0 is treated as the last day of the previous month.
Change the date or the amount to see how the date rolls over.
new Date(2024, 2, 0)も同じく「2024 年 3 月 0 日」を指定しているため、2024 年 2 月 29 日になります。
12 月の翌月も自動で繰り上がる
Section titled “12 月の翌月も自動で繰り上がる”12 月(getMonth()が11)に 1 を足すと月の値は12になりますが、これも存在しない月として翌年の 1 月に繰り上げられます。そのため、12 月の場合も特別扱いする必要はありません。
date.setMonth(date.getMonth() + 1)
- Original date20241215
- Date given by setMonth(12)20241315Does not exist
- Actual date2025115
- Months are counted from 0, so 11 means month 12.
- Month 13 does not exist, so it is treated as month 1 of 2025.
Change the date or the amount to see how the date rolls over.
/** 2024 年 12 月の末日(2025 年 1 月 0 日 = 2024 年 12 月 31 日) */console.log(new Date(2024, 11 + 1, 0).toLocaleDateString()); // 2024/12/31月初日を取得する
Section titled “月初日を取得する”月初日は、日に1を指定するだけで求められます。
const now = new Date();
/** 今月の初日 */const startOfMonth = new Date(now.getFullYear(), now.getMonth(), 1);前月・翌月の末日を取得する
Section titled “前月・翌月の末日を取得する”月の値を変えることで、前月や翌月の末日も求められます。
const now = new Date();
/** 前月の末日(今月の 0 日) */const endOfLastMonth = new Date(now.getFullYear(), now.getMonth(), 0);
/** 翌月の末日(翌々月の 0 日) */const endOfNextMonth = new Date(now.getFullYear(), now.getMonth() + 2, 0);月の日数を取得する
Section titled “月の日数を取得する”月末日の「日」は、その月の日数と同じです。getDateメソッドで取り出すと、月の日数として使えます。
function main(workbook: ExcelScript.Workbook) { /** 対象の年 */ const year = 2024;
for (let month = 0; month < 12; month++) { /** 月の日数(月末日の「日」) */ const days = new Date(year, month + 1, 0).getDate();
console.log(`${month + 1}月は${days}日まであります`); }}1月は31日まであります2月は29日まであります3月は31日まであります…(中略)12月は31日まであります関数にまとめる
Section titled “関数にまとめる”月末日を何度も使う場合は、関数にまとめておくと便利です。元のDateオブジェクトを変更せずに、新しいDateオブジェクトを返すようにしています。
function main(workbook: ExcelScript.Workbook) { const date = new Date(2024, 1, 10);
console.log(getEndOfMonth(date).toLocaleDateString()); // 2024/2/29 console.log(date.toLocaleDateString()); // 2024/2/10(元の日付は変わらない)}
/** 指定した日付が属する月の末日を返す */function getEndOfMonth(date: Date): Date { return new Date(date.getFullYear(), date.getMonth() + 1, 0);}