n か月後の日付
This content is not available in your language yet.
ここではオフィススクリプトを使って、1 か月後や n か月後の日付を求める方法を、サンプルコードと併せて解説します。
setMonth で月を加算する
Section titled “setMonth で月を加算する”DateオブジェクトのgetMonthメソッドで現在の月を取得し、加算した値をsetMonthメソッドに渡します。
function main(workbook: ExcelScript.Workbook) { const date = new Date(2024, 3, 15); // 2024 年 4 月 15 日
date.setMonth(date.getMonth() + 1);
console.log(date.toLocaleDateString()); // 2024/5/15}12 月に月を加算した場合も、自動的に翌年に繰り上げられます。日付が 28 日以前であれば、この方法で問題ありません。
月末の日付に加算すると翌々月にずれる
Section titled “月末の日付に加算すると翌々月にずれる”元の日付が 29 日以降の場合、加算した先の月にその日が存在しないことがあります。
以下は 2024 年 1 月 31 日に 1 か月を加算した場合の例です。setMonth(1)で「2024 年 2 月 31 日」が指定されますが、2 月は 29 日までのため、超えた 2 日分が 3 月に繰り越されます。
日付や加える数を変えて、どのような場合にずれが起きるかを確かめてみましょう。
date.setMonth(date.getMonth() + 1)
- Original date2024131
- Date given by setMonth(1)2024231Does not exist
- Actual date202432
- Months are counted from 0, so 0 means month 1.
- Month 2 of 2024 has only 29 days, so the extra 2 days roll over into the next month.
Change the date or the amount to see how the date rolls over.
月を減算する場合も同じです。3 月 31 日の 1 か月前は「2 月 31 日」となり、2 月ではなく 3 月 2 日になります。
date.setMonth(date.getMonth() - 1)
- Original date2024331
- Date given by setMonth(1)2024231Does not exist
- Actual date202432
- Months are counted from 0, so 2 means month 3.
- Month 2 of 2024 has only 29 days, so the extra 2 days roll over into the next month.
Change the date or the amount to see how the date rolls over.
締め日や支払期日の計算でこのずれが起きると、1 か月以上先の日付として扱われてしまいます。
月末を超えないように加算する
Section titled “月末を超えないように加算する”VBA のDateAdd関数と同じく、存在しない日付になる場合は月の末日に合わせる関数を作成します。
function main(workbook: ExcelScript.Workbook) { const date = new Date(2024, 0, 31); // 2024 年 1 月 31 日
console.log(addMonths(date, 1).toLocaleDateString()); // 2024/2/29 console.log(addMonths(date, 2).toLocaleDateString()); // 2024/3/31 console.log(addMonths(date, 3).toLocaleDateString()); // 2024/4/30 console.log(addMonths(date, -1).toLocaleDateString()); // 2023/12/31}
/** * n か月後の日付を返す(存在しない日付になる場合は、その月の末日にする) * @param date 元の日付(変更されない) * @param months 加算する月数(負数なら n か月前) */function addMonths(date: Date, months: number): Date { /** 加算した先の月の初日 */ const firstDay = new Date(date.getFullYear(), date.getMonth() + months, 1);
/** 加算した先の月の末日の「日」(翌月の 0 日) */ const lastDay = new Date(firstDay.getFullYear(), firstDay.getMonth() + 1, 0).getDate();
return new Date(firstDay.getFullYear(), firstDay.getMonth(), Math.min(date.getDate(), lastDay));}処理の流れは次のとおりです。
- 加算した先の月の 初日 を作成します。1 日はどの月にも存在するため、ここでずれは起きません。
- その月の末日を求めます(翌月の 0 日)。
- 元の日付の「日」と月末の「日」のうち、小さい方を日として設定します。
月末日の求め方は、以下の記事で詳しく解説しています。
1 年後の日付
Section titled “1 年後の日付”年を加算するsetFullYearメソッドでも、同じずれが起きます。うるう日(2 月 29 日)に 1 年を加算すると、翌年に 2 月 29 日が存在しないため 3 月 1 日になります。
date.setFullYear(date.getFullYear() + 1)
- Original date2024229
- Date given by setFullYear(2025)2025229Does not exist
- Actual date202531
- Month 2 of 2025 has only 28 days, so the extra 1 days roll over into the next month.
Change the date or the amount to see how the date rolls over.
月末に合わせたい場合は、先ほどのaddMonths関数に 12 か月を指定します。
const leapDay = new Date(2024, 1, 29); // 2024 年 2 月 29 日
console.log(addMonths(leapDay, 12).toLocaleDateString()); // 2025/2/28