LoginSignup
2
1

More than 3 years have passed since last update.

【C#】指定された月から秒単位までで月初と月末を取得する

Posted at

やること

DateTimeの年月の情報から、秒単位までの月初~月末を取得します。

やり方

月初はnew DateTime(年, 月, 1)で0時0分0秒として取得できます。
月末は月初に1月足して1ms引く(-1ms足す)ことで求められます。

private Tuple<DateTime, DateTime> getPeriod(DateTime targetMonth)
{
    var start = new DateTime(targetMonth.Year, targetMonth.Month, 1);
    var end = start.AddMonths(1).AddMilliseconds(-1.0);
    return new Tuple<DateTime, DateTime>(start, end);
}

参考にさせていただいた記事

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1