[C#]日付の時分秒の加算、減算のサンプル
構文
1.秒の加算・減算
日付.AddSeconds(加減数)
2.分の加算・減算
日付.AddMinutes(加減数)
3.時間の加算・減算
日付.AddHours(加減数)
使用例
using System;
class Arkgame
{
public static void Main()
{
DateTime today = DateTime.Today;
//3時間の加算
DateTime res1 = today.AddHours(3);
//3時間の減算
DateTime res2 = today.AddHours(-3);
Console.WriteLine("時間の加算減算の結果");
Console.WriteLine("元の日付: " + today.ToString());
Console.WriteLine("時間の加算: " + res1.ToString());
Console.WriteLine("時間の減算: " + res2.ToString());
Console.WriteLine();
DateTime today2 = DateTime.Today;
//3分の加算
DateTime res3 = today2.AddMinutes(3);
//3分の減算
DateTime res4 = today2.AddMinutes(-3);
Console.WriteLine("分の加算減算の結果");
Console.WriteLine("元の日付: " + today2.ToString());
Console.WriteLine("分の加算: " + res3.ToString());
Console.WriteLine("分の減算: " + res4.ToString());
Console.WriteLine();
DateTime today3 = DateTime.Today;
//3秒の加算
DateTime res5 = today2.AddSeconds(3);
//3秒の減算
DateTime res6 = today2.AddSeconds(-3);
Console.WriteLine("秒の加算減算の結果");
Console.WriteLine("元の日付: " + today3.ToString());
Console.WriteLine("秒の加算: " + res5.ToString());
Console.WriteLine("秒の減算: " + res6.ToString());
Console.ReadKey();
}
}
using System;
class Arkgame
{
public static void Main()
{
DateTime today = DateTime.Today;
//3時間の加算
DateTime res1 = today.AddHours(3);
//3時間の減算
DateTime res2 = today.AddHours(-3);
Console.WriteLine("時間の加算減算の結果");
Console.WriteLine("元の日付: " + today.ToString());
Console.WriteLine("時間の加算: " + res1.ToString());
Console.WriteLine("時間の減算: " + res2.ToString());
Console.WriteLine();
DateTime today2 = DateTime.Today;
//3分の加算
DateTime res3 = today2.AddMinutes(3);
//3分の減算
DateTime res4 = today2.AddMinutes(-3);
Console.WriteLine("分の加算減算の結果");
Console.WriteLine("元の日付: " + today2.ToString());
Console.WriteLine("分の加算: " + res3.ToString());
Console.WriteLine("分の減算: " + res4.ToString());
Console.WriteLine();
DateTime today3 = DateTime.Today;
//3秒の加算
DateTime res5 = today2.AddSeconds(3);
//3秒の減算
DateTime res6 = today2.AddSeconds(-3);
Console.WriteLine("秒の加算減算の結果");
Console.WriteLine("元の日付: " + today3.ToString());
Console.WriteLine("秒の加算: " + res5.ToString());
Console.WriteLine("秒の減算: " + res6.ToString());
Console.ReadKey();
}
}
using System; class Arkgame { public static void Main() { DateTime today = DateTime.Today; //3時間の加算 DateTime res1 = today.AddHours(3); //3時間の減算 DateTime res2 = today.AddHours(-3); Console.WriteLine("時間の加算減算の結果"); Console.WriteLine("元の日付: " + today.ToString()); Console.WriteLine("時間の加算: " + res1.ToString()); Console.WriteLine("時間の減算: " + res2.ToString()); Console.WriteLine(); DateTime today2 = DateTime.Today; //3分の加算 DateTime res3 = today2.AddMinutes(3); //3分の減算 DateTime res4 = today2.AddMinutes(-3); Console.WriteLine("分の加算減算の結果"); Console.WriteLine("元の日付: " + today2.ToString()); Console.WriteLine("分の加算: " + res3.ToString()); Console.WriteLine("分の減算: " + res4.ToString()); Console.WriteLine(); DateTime today3 = DateTime.Today; //3秒の加算 DateTime res5 = today2.AddSeconds(3); //3秒の減算 DateTime res6 = today2.AddSeconds(-3); Console.WriteLine("秒の加算減算の結果"); Console.WriteLine("元の日付: " + today3.ToString()); Console.WriteLine("秒の加算: " + res5.ToString()); Console.WriteLine("秒の減算: " + res6.ToString()); Console.ReadKey(); } }
実行結果
時間の加算減算の結果
元の日付: 2021/10/08 0:00:00
時間の加算: 2021/10/08 3:00:00
時間の減算: 2021/10/07 21:00:00
分の加算減算の結果
元の日付: 2021/10/08 0:00:00
分の加算: 2021/10/08 0:03:00
分の減算: 2021/10/07 23:57:00
秒の加算減算の結果
元の日付: 2021/10/08 0:00:00
秒の加算: 2021/10/08 0:00:03
秒の減算: 2021/10/07 23:59:57
時間の加算減算の結果
元の日付: 2021/10/08 0:00:00
時間の加算: 2021/10/08 3:00:00
時間の減算: 2021/10/07 21:00:00
分の加算減算の結果
元の日付: 2021/10/08 0:00:00
分の加算: 2021/10/08 0:03:00
分の減算: 2021/10/07 23:57:00
秒の加算減算の結果
元の日付: 2021/10/08 0:00:00
秒の加算: 2021/10/08 0:00:03
秒の減算: 2021/10/07 23:59:57
時間の加算減算の結果 元の日付: 2021/10/08 0:00:00 時間の加算: 2021/10/08 3:00:00 時間の減算: 2021/10/07 21:00:00 分の加算減算の結果 元の日付: 2021/10/08 0:00:00 分の加算: 2021/10/08 0:03:00 分の減算: 2021/10/07 23:57:00 秒の加算減算の結果 元の日付: 2021/10/08 0:00:00 秒の加算: 2021/10/08 0:00:03 秒の減算: 2021/10/07 23:59:57