日付(DateTime)を加算・減算するサンプル

  1. using System;
  2. namespace ConsoleApplication1
  3. {
  4.     class Program
  5.     {
  6.         static void Main(string[] args)
  7.         {
  8.             String date1 = "2013/04/01 23:59:01";
  9.             DateTime dt1 = DateTime.Parse(date1);
  10.             // 1日加算。年はAddYears、月はAddMonths、日はAddDays、時はAddHours、分はAddMinutes、秒はAddSeconds、ミリ秒はAddMilliseconds
  11.             dt1 = dt1.AddDays(1);
  12.             // DateTimeの日付を、指定した形式で出力
  13.             Console.WriteLine("2013/04/01 23:59:01.AddDays(1) → " + dt1);
  14.             // 元の日付に戻しておく
  15.             dt1 = DateTime.Parse(date1);
  16.             // 1日減算。
  17.             dt1 = dt1.AddDays(-1);
  18.             Console.WriteLine("2013/04/01 23:59:01.AddDays(-1) → " + dt1);
  19.             Console.ReadLine();
  20.         }
  21.     }
  22. }

image

実行ファイルダウンロード

戻る