
- C# DateTime 型の基本操作:日付と時刻を自在に操る
C# DateTime 型の基本操作:日付と時刻を自在に操る
C# において、日付と時刻を扱うための中心的な構造体が System.DateTime です。本記事では、DateTime 型の基本的な操作について解説し、日付や時刻の取得、作成、演算、フォーマットといった、プログラミングで頻繁に行う処理をマスターしましょう。
1. DateTime オブジェクトの取得
DateTime オブジェクトを取得する基本的な方法をいくつかご紹介します。
現在の日付と時刻を取得する:DateTime.Now
現在の日付と時刻(ローカルタイムゾーン)を取得するには、静的プロパティ DateTime.Now を使用します。
DateTime now = DateTime.Now;
Console.WriteLine($"現在の日時: {now}");
UTC (協定世界時) の現在の日付と時刻を取得する:DateTime.UtcNow
UTC 基準の現在の日付と時刻を取得するには、静的プロパティ DateTime.UtcNow を使用します。
DateTime utcNow = DateTime.UtcNow;
Console.WriteLine($"UTC の現在日時: {utcNow}");
今日、明日、昨日の日付を取得する:DateTime.Today
今日の日付(時刻部分は 00:00:00)を取得するには、静的プロパティ DateTime.Today を使用します。明日や昨日の日付は、AddDays() メソッドと組み合わせて取得できます。
DateTime today = DateTime.Today;
Console.WriteLine($"今日の日付: {today}");
DateTime tomorrow = today.AddDays(1);
Console.WriteLine($"明日の日付: {tomorrow}");
DateTime yesterday = today.AddDays(-1);
Console.WriteLine($"昨日の日付: {yesterday}");
2. DateTime オブジェクトの作成
特定の日付や時刻を持つ DateTime オブジェクトを作成する方法をいくつかご紹介します。
コンストラクタを使用する
DateTime 構造体のコンストラクタを使用して、年、月、日、時、分、秒などを指定して DateTime オブジェクトを作成できます。
// 年、月、日を指定
DateTime specificDate1 = new DateTime(2025, 4, 15);
Console.WriteLine($"特定の日付 (年/月/日): {specificDate1}"); // 2025/04/15 0:00:00
// 年、月、日、時、分、秒を指定
DateTime specificDateTime2 = new DateTime(2025, 4, 15, 10, 30, 0);
Console.WriteLine($"特定の日時 (年/月/日/時/分/秒): {specificDateTime2}"); // 2025/04/15 10:30:00
文字列から解析する:DateTime.Parse() / DateTime.TryParse() / DateTime.ParseExact()
文字列として表現された日付や時刻を DateTime オブジェクトに変換する方法は、以前のブログ記事で詳しく解説しました。
- DateTime.Parse(): 一般的な形式の文字列を解析します。形式が不正な場合は例外が発生します。
- DateTime.TryParse(): 解析を試み、成功/失敗を
boolで返します。例外は発生しません。 - DateTime.ParseExact(): 特定の形式に合致する文字列のみを解析します。形式が不正な場合は例外が発生します。
string dateString = "2025-04-15 15:45:30";
DateTime parsedDateTime = DateTime.Parse(dateString);
Console.WriteLine($"文字列から解析: {parsedDateTime}");
3. DateTime オブジェクトの演算
DateTime オブジェクトに対して、日付や時刻の加算・減算などの演算を行うことができます。
日数を加算・減算する:AddDays()
DateTime futureDate = now.AddDays(7); // 7日後の日付
Console.WriteLine($"7日後の日付: {futureDate}");
DateTime pastDate = now.AddDays(-3); // 3日前の日付
Console.WriteLine($"3日前の日付: {pastDate}");
時間、分、秒、ミリ秒などを加算・減算する:AddTimeSpan() / AddHours() / AddMinutes() / AddSeconds() / AddMilliseconds()
TimeSpan duration = new TimeSpan(2, 30, 0); // 2時間30分
DateTime futureTime = now.AddTimeSpan(duration);
Console.WriteLine($"2時間30分後の日時: {futureTime}");
DateTime laterTime = now.AddHours(1.5); // 1時間30分後
Console.WriteLine($"1時間30分後の日時: {laterTime}");
年、月を加算・減算する:AddYears() / AddMonths()
DateTime nextYear = now.AddYears(1);
Console.WriteLine($"1年後の日時: {nextYear}");
DateTime previousMonth = now.AddMonths(-1);
Console.WriteLine($"1ヶ月前の日時: {previousMonth}");
4. DateTime オブジェクトのフォーマット
DateTime オブジェクトを様々な形式の文字列に変換することができます。これには ToString() メソッドと、標準またはカスタムのフォーマット指定子を使用します。
Console.WriteLine(now.ToString("yyyy年MM月dd日(ddd) HH時mm分ss秒"));
// 出力例: 2025年04月15日(火) 10時30分00秒
文字列への変換方法は以下の記事で紹介しておりますので是非ご覧ください。
まとめ
System.DateTime 構造体は、C# で日付と時刻を扱うための強力なツールです。この記事で解説した基本的な操作を理解することで、日付や時刻の取得、作成、演算、そして様々な形式での表示を自由自在に行うことができるようになります。これらの知識を活用して、より高度な日付と時刻の処理を実装していきましょう。