今月の月末の値のDateTimeオブジェクトを取得するコードを紹介します。
概要
当月の月末の日付を表すDateTimeオブジェクトを取得したい場合があります。
月末のDateTimeオブジェクトを取得するコードを紹介します。
プログラム1: 月の日数を求める方法
当月の日数を求めて、月末の日付を取得してDateTimeオブジェクトを取得する方法です。
Windows Formアプリケーションを作成します。
UI
下図のフォームを作成します。ボタンが2つありますが左のボタンはこのプログラムでは利用しません。
コード
下記のコードを記述します。ボタンのClickイベントを実装します。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace CalcDateTime2020
{
public partial class FormCalcMonth : Form
{
public FormCalcMonth()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
int this_month_days = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
DateTime dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month, this_month_days);
textBox1.Text = dt.ToString("yyyy/MM/dd");
}
}
}
解説
DateTime.DaysInMonth
メソッドを呼び出し指定した年月の月の日数を求めます。DaysInMonthメソッドの詳細は
こちらの記事を参照してください。
int this_month_days = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);
new 演算子を利用してDateTimeオブジェクトを作成します。コンストラクタの引数に作成したい日時の年、月、日を与えます。年、月は
DateTime.Now
プロパティを利用して
現在の日付の年と月を与えます。日付は先ほどDaysInMonthメソッドで求めた、月の日数を与えることで月末の日付を表すDateTimeオブジェクトを作成します。
DateTime dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month, this_month_days);
作成したDateTimeオブジェクトの値をテキストボックスに表示します。
textBox1.Text = dt.ToString("yyyy/MM/dd");
プログラム2: 翌月の月初から1日引く方法
翌月の月初のDateTimeオブジェクトを作成して、1日引いてDateTimeオブジェクトを取得する方法です。
UI
下図のフォームを作成します。先のプログラム例と同じです。ボタンが2つありますが左のボタンはこのプログラムでは利用しません。
コード
下記のコードを記述します。ボタンのClickイベントを実装します。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace CalcDateTime2020
{
public partial class FormCalcMonth : Form
{
public FormCalcMonth()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
DateTime next_month = DateTime.Now.AddMonths(1);
DateTime next_month_beginning = new DateTime(next_month.Year, next_month.Month, 1);
DateTime this_month_ending = next_month_beginning.AddDays(-1);
textBox1.Text = this_month_ending.ToString("yyyy/MM/dd");
}
}
}
解説
現在の日付の1か月後の日付を取得します。
DateTime next_month = DateTime.Now.AddMonths(1);
翌月の年、月の値を利用して、new演算子を利用して翌月の月初の日付を作成します。
DateTime next_month_beginning = new DateTime(next_month.Year, next_month.Month, 1);
AddDays
メソッドを利用して、翌月の月初の日付から1日前の日付を取得します。この処理で当月の月末の日付が取得できます。
DateTime this_month_ending = next_month_beginning.AddDays(-1);
取得したDateTimeオブジェクトの値をテキストボックスに表示します。
textBox1.Text = this_month_ending.ToString("yyyy/MM/dd");
補足
8月31日に1か月を加算した場合、9月30日となります。(9月31日となり、10月1日の値になってしまうことはありません)
DateTime dt = new DateTime(2020,8,31);
dt = dt.AddMonths(1);
textBox1.Text = dt.ToString("yyyy/MM/dd");
そのため、下記のコードのような記述は必要ありません。(このコードでも正しく動作はします。)
DateTime dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
DateTime next_month = dt.AddMonths(1);
実行結果
プロジェクトを実行します。下図のウィンドウが表示されます。
[今月の月末の日付]のボタンをクリックします。今月の月末の日付がテキストボックスに表示されます。
月末の日付を表すDateTimeオブジェクトが取得できました。
著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
最終更新日: 2020-08-10
作成日: 2020-07-31