[C#]年月日の加算、減算処理のサンプル
書式
1.日の加算・減算
日付.AddDays(加減数)
2.月の加算・減算
日付.AddMonths(加減数)
3.年の加算・減算
日付.AddYears(加減数)
使用例
using System;
class Arkgame
{
    public static void Main()
    {
        DateTime today = DateTime.Today;
        //3年の加算
        DateTime res1 = today.AddYears(3);
        //3年の減算
        DateTime res2 = today.AddYears(-3);
        Console.WriteLine("年の加算減算の結果");
        Console.WriteLine("元の日付: " + today.ToString());
        Console.WriteLine("年の加算: " + res1.ToString());
        Console.WriteLine("年の減算: " + res2.ToString());
        Console.WriteLine();
        Console.WriteLine("月の加算減算の結果");
        DateTime today2 = DateTime.Today;
        //3月の加算
        DateTime res3 = today2.AddMonths(3);
        //3月の減算
        DateTime res4 = today2.AddMonths(-3);
        Console.WriteLine("元の日付: " + today2.ToString());
        Console.WriteLine("月の加算: " + res3.ToString());
        Console.WriteLine("月の減算: " + res4.ToString());
        Console.WriteLine();
        Console.WriteLine("日の加算減算の結果");
        DateTime today3 = DateTime.Today;
        //3日の加算
        DateTime res5 = today2.AddDays(3);
        //3日の減算
        DateTime res6 = today2.AddDays(-3);
        Console.WriteLine("元の日付: " + today3.ToString());
        Console.WriteLine("日の加算: " + res5.ToString());
        Console.WriteLine("日の減算: " + res6.ToString());
        Console.ReadKey();
    }
}
実行結果
年の加算減算の結果 元の日付: 2021/10/08 0:00:00 年の加算: 2024/10/08 0:00:00 年の減算: 2018/10/08 0:00:00 月の加算減算の結果 元の日付: 2021/10/08 0:00:00 月の加算: 2022/01/08 0:00:00 月の減算: 2021/07/08 0:00:00 日の加算減算の結果 元の日付: 2021/10/08 0:00:00 日の加算: 2021/10/11 0:00:00 日の減算: 2021/10/05 0:00:00