.NETヘルプ C#のDatetime Objects(開発者向けの動作方法) Jacob Mellor 更新日:6月 22, 2025 IronPDF をダウンロード NuGet ダウンロード DLL ダウンロード Windows 版 無料トライアル LLM向けのコピー LLM向けのコピー LLM 用の Markdown としてページをコピーする ChatGPTで開く このページについてChatGPTに質問する ジェミニで開く このページについてGeminiに問い合わせる Grokで開く このページについてGrokに質問する 困惑の中で開く このページについてPerplexityに問い合わせる 共有する Facebook で共有 Xでシェア(Twitter) LinkedIn で共有 URLをコピー 記事をメールで送る DateTime オブジェクトは、.NET Framework アプリケーションで日付と時刻を扱うための基本機能です。 それらは、日付と時刻を操作、フォーマット、および比較するための強力な機能を提供します。 この記事は、C# の DateTime オブジェクトの包括的な概要を提供し、作成、操作、フォーマット、および一般的な使用例をカバーすることを目的としています。 この記事の最後には、IronPDF が C# アプリケーション内でオンザフライで PDF ドキュメントを生成する方法を探ります。 DateTime オブジェクトの作成 C# で DateTime オブジェクトを作成するのは簡単です。 DateTime オブジェクトを異なるパラメーターで初期化するためのいくつかのコンストラクターがあります。 // Current date and time DateTime currentDateTime = DateTime.Now; // Specific date and time DateTime specificDateTime = new DateTime(2024, 3, 16, 10, 30, 0); // Date only DateTime dateOnly = DateTime.Today; // Date and time in UTC DateTime utcDateTime = DateTime.UtcNow; // Current date and time DateTime currentDateTime = DateTime.Now; // Specific date and time DateTime specificDateTime = new DateTime(2024, 3, 16, 10, 30, 0); // Date only DateTime dateOnly = DateTime.Today; // Date and time in UTC DateTime utcDateTime = DateTime.UtcNow; $vbLabelText $csharpLabel DateTime オブジェクトの操作 DateTime オブジェクトは、時間の加減算、コンポーネントの抽出、タイムゾーン間の変換など、日付と時刻を操作するためのさまざまなメソッドを提供します。 DateTime now = DateTime.Now; // Adding days DateTime futureDate = now.AddDays(7); // Subtracting hours DateTime pastTime = now.AddHours(-3); // Getting components int year = now.Year; int month = now.Month; int day = now.Day; int hour = now.Hour; int minute = now.Minute; int second = now.Second; // Converting between time zones DateTime utcTime = DateTime.UtcNow; DateTime localTime = utcTime.ToLocalTime(); DateTime now = DateTime.Now; // Adding days DateTime futureDate = now.AddDays(7); // Subtracting hours DateTime pastTime = now.AddHours(-3); // Getting components int year = now.Year; int month = now.Month; int day = now.Day; int hour = now.Hour; int minute = now.Minute; int second = now.Second; // Converting between time zones DateTime utcTime = DateTime.UtcNow; DateTime localTime = utcTime.ToLocalTime(); $vbLabelText $csharpLabel DateTime オブジェクトのフォーマット DateTime オブジェクトは、さまざまなフォーマット指定子を使用して文字列にフォーマットされ、必要な形式で表現されます。 DateTime dateTime = DateTime.Now; // Standard date and time format string standardFormat = dateTime.ToString("G"); // Custom format string customFormat = dateTime.ToString("dd/MM/yyyy HH:mm:ss"); // Format for sorting string sortableFormat = dateTime.ToString("yyyy-MM-ddTHH:mm:ss"); DateTime dateTime = DateTime.Now; // Standard date and time format string standardFormat = dateTime.ToString("G"); // Custom format string customFormat = dateTime.ToString("dd/MM/yyyy HH:mm:ss"); // Format for sorting string sortableFormat = dateTime.ToString("yyyy-MM-ddTHH:mm:ss"); $vbLabelText $csharpLabel DateTime オブジェクトの比較 C# では、<, >, <=, >=, ==, != といった標準の比較演算子が提供され、DateTime オブジェクトを直接比較できます。 これらの演算子は、DateTime オブジェクトの基礎であるティックを比較します。これにより、グレゴリオ暦で西暦 1 年 1 月 1 日 00:00:00.000 から経過した 100 ナノ秒間隔の数が表されます。 以下は、比較演算子の使用を示す例です。 DateTime date1 = DateTime.Now; DateTime date2 = DateTime.Now.AddDays(1); if (date1 < date2) { Console.WriteLine("date1 is earlier than date2."); } else if (date1 > date2) { Console.WriteLine("date1 is later than date2."); } else { Console.WriteLine("date1 is equal to date2."); } DateTime date1 = DateTime.Now; DateTime date2 = DateTime.Now.AddDays(1); if (date1 < date2) { Console.WriteLine("date1 is earlier than date2."); } else if (date1 > date2) { Console.WriteLine("date1 is later than date2."); } else { Console.WriteLine("date1 is equal to date2."); } $vbLabelText $csharpLabel DateTime.Compare C# メソッドの使用 比較演算子に加えて、DateTime オブジェクトは、それらのオブジェクト間の相対値を比較するためのメソッドも提供します。 これらのメソッドは、特定の状況でより柔軟性と可読性を提供します。 CompareTo() メソッドは、2 つの DateTime オブジェクトを比較し、どちらが早いか、後のか、または同じかを示す整数値を返します。 DateTime date1 = DateTime.Now; DateTime date2 = DateTime.Now.AddDays(1); int result = date1.CompareTo(date2); if (result < 0) { Console.WriteLine("date1 is earlier than date2."); } else if (result > 0) { Console.WriteLine("date1 is later than date2."); } else { Console.WriteLine("date1 is equal to date2."); } DateTime date1 = DateTime.Now; DateTime date2 = DateTime.Now.AddDays(1); int result = date1.CompareTo(date2); if (result < 0) { Console.WriteLine("date1 is earlier than date2."); } else if (result > 0) { Console.WriteLine("date1 is later than date2."); } else { Console.WriteLine("date1 is equal to date2."); } $vbLabelText $csharpLabel トレランスを考慮した DateTime オブジェクトの比較 DateTime オブジェクトを比較するには、特に時間間隔を含む計算を行う場合、精度の違いにより許容範囲を考慮することが重要です。 これは、2 つの DateTime 値の絶対的な差を定義された許容範囲のしきい値と比較することで実現できます。 class Program { public static void Main() { DateTime date1 = DateTime.Now; DateTime date2 = DateTime.Now.AddMilliseconds(10); TimeSpan tolerance = TimeSpan.FromMilliseconds(5); bool isEqual = Math.Abs((date1 - date2).TotalMilliseconds) <= tolerance.TotalMilliseconds; if (isEqual) { Console.WriteLine("date1 is considered equal to date2 within the tolerance."); } else { Console.WriteLine("date1 is not equal to date2 within the tolerance."); } } } class Program { public static void Main() { DateTime date1 = DateTime.Now; DateTime date2 = DateTime.Now.AddMilliseconds(10); TimeSpan tolerance = TimeSpan.FromMilliseconds(5); bool isEqual = Math.Abs((date1 - date2).TotalMilliseconds) <= tolerance.TotalMilliseconds; if (isEqual) { Console.WriteLine("date1 is considered equal to date2 within the tolerance."); } else { Console.WriteLine("date1 is not equal to date2 within the tolerance."); } } } $vbLabelText $csharpLabel タイムゾーンと夏時間の取り扱い C# の DateTime オブジェクトは、ローカルタイムと協定世界時 (UTC) の両方を表すことができます。 特にグローバルなアプリケーションを扱う際は、タイムゾーンの変換に注意することが重要です。 DateTime localTime = DateTime.Now; DateTime utcTime = DateTime.UtcNow; Console.WriteLine("Local Time: " + localTime); Console.WriteLine("UTC Time: " + utcTime); DateTime localTime = DateTime.Now; DateTime utcTime = DateTime.UtcNow; Console.WriteLine("Local Time: " + localTime); Console.WriteLine("UTC Time: " + utcTime); $vbLabelText $csharpLabel IronPDF を使用した C# での PDF ドキュメント生成 IronPDF は、Iron Software からの効率的で使いやすい PDF 生成ライブラリです。 NuGet パッケージ マネージャーを使用してインストールできます。 dotnet add package IronPdf --version 2024.3.4 または、以下に示すように Visual Studio から直接インストールできます。 では、DateTime オブジェクトをデモするための PDF 生成に進みましょう。 using IronPdf; class Program { static void Main() { Console.WriteLine("-----------Iron Software-------------"); // Create a new instance of ChromePdfRenderer var renderer = new ChromePdfRenderer(); // HTML content for the PDF var content = "<h1> Iron Software is Awesome </h1> Made with IronPDF!"; content += "<h2>Demo Datetime Objects in C#</h2>"; // Current date and time content += "<h3>Current date and time</h3>"; DateTime currentDateTime = DateTime.Now; content += $"<p>Current date and time: {currentDateTime:U}</p>"; Console.WriteLine($"Current date and time: {currentDateTime:U}"); // Specific date and time content += "<h3>Specific date and time</h3>"; DateTime specificDateTime = new DateTime(2024, 3, 16, 10, 30, 0); content += $"<p>Specific date and time: {specificDateTime:U}</p>"; Console.WriteLine($"Specific date and time: {specificDateTime:U}"); // Date only content += "<h3>Date Only</h3>"; DateTime dateOnly = DateTime.Today; content += $"<p>Date only: {dateOnly:U}</p>"; Console.WriteLine($"Date only: {dateOnly:U}"); // Date and time in UTC content += "<h3>Date and time in UTC</h3>"; DateTime utcDateTime = DateTime.UtcNow; content += $"<p>Date and time in UTC: {utcDateTime:U}</p>"; Console.WriteLine($"Date and time in UTC: {utcDateTime:U}"); // Compare dates with Operators content += "<h3>Compare dates with Operators</h3>"; DateTime date1 = DateTime.Now; DateTime date2 = DateTime.Now.AddDays(1); content += $"<p>Compare date1 {date1:d}, date2 {date2:d}: {CompareDates(date1, date2)}</p>"; Console.WriteLine($"Compare date1 {date1:U}, date2 {date2:U}: {CompareDates(date1, date2)}"); // Compare dates with Compare Method content += "<h3>Compare dates with Compare Method</h3>"; content += $"<p>Compare date1 {date1:d}, date2 {date2:d}: {CompareDatesWithCompare(date1, date2)}</p>"; Console.WriteLine($"Compare date1 {date1:U}, date2 {date2:U}: {CompareDatesWithCompare(date1, date2)}"); // Render the content to PDF var pdf = renderer.RenderHtmlAsPdf(content); // Save the PDF to the output file pdf.SaveAs("outputDate.pdf"); } // Compare two dates using CompareTo method public static string CompareDatesWithCompare(DateTime date1, DateTime date2) { int result = date1.CompareTo(date2); string resultString; if (result < 0) { resultString = "date1 is earlier than date2."; Console.WriteLine(resultString); } else if (result > 0) { resultString = "date1 is later than date2."; Console.WriteLine(resultString); } else { resultString = "date1 is equal to date2."; Console.WriteLine(resultString); } return resultString; } // Compare two dates using basic comparison operators public static string CompareDates(DateTime date1, DateTime date2) { string result; if (CheckLessor(date1, date2)) { result = "date1 is earlier than date2."; Console.WriteLine(result); } else if (CheckGreater(date1, date2)) { result = "date1 is later than date2."; Console.WriteLine(result); } else { result = "date1 is equal to date2."; Console.WriteLine(result); } return result; } // Helper method to check if the first date is greater than the second date public static bool CheckGreater(DateTime date1, DateTime date2) { return date1 > date2; } // Helper method to check if the first date is less than the second date public static bool CheckLessor(DateTime date1, DateTime date2) { return date1 < date2; } } using IronPdf; class Program { static void Main() { Console.WriteLine("-----------Iron Software-------------"); // Create a new instance of ChromePdfRenderer var renderer = new ChromePdfRenderer(); // HTML content for the PDF var content = "<h1> Iron Software is Awesome </h1> Made with IronPDF!"; content += "<h2>Demo Datetime Objects in C#</h2>"; // Current date and time content += "<h3>Current date and time</h3>"; DateTime currentDateTime = DateTime.Now; content += $"<p>Current date and time: {currentDateTime:U}</p>"; Console.WriteLine($"Current date and time: {currentDateTime:U}"); // Specific date and time content += "<h3>Specific date and time</h3>"; DateTime specificDateTime = new DateTime(2024, 3, 16, 10, 30, 0); content += $"<p>Specific date and time: {specificDateTime:U}</p>"; Console.WriteLine($"Specific date and time: {specificDateTime:U}"); // Date only content += "<h3>Date Only</h3>"; DateTime dateOnly = DateTime.Today; content += $"<p>Date only: {dateOnly:U}</p>"; Console.WriteLine($"Date only: {dateOnly:U}"); // Date and time in UTC content += "<h3>Date and time in UTC</h3>"; DateTime utcDateTime = DateTime.UtcNow; content += $"<p>Date and time in UTC: {utcDateTime:U}</p>"; Console.WriteLine($"Date and time in UTC: {utcDateTime:U}"); // Compare dates with Operators content += "<h3>Compare dates with Operators</h3>"; DateTime date1 = DateTime.Now; DateTime date2 = DateTime.Now.AddDays(1); content += $"<p>Compare date1 {date1:d}, date2 {date2:d}: {CompareDates(date1, date2)}</p>"; Console.WriteLine($"Compare date1 {date1:U}, date2 {date2:U}: {CompareDates(date1, date2)}"); // Compare dates with Compare Method content += "<h3>Compare dates with Compare Method</h3>"; content += $"<p>Compare date1 {date1:d}, date2 {date2:d}: {CompareDatesWithCompare(date1, date2)}</p>"; Console.WriteLine($"Compare date1 {date1:U}, date2 {date2:U}: {CompareDatesWithCompare(date1, date2)}"); // Render the content to PDF var pdf = renderer.RenderHtmlAsPdf(content); // Save the PDF to the output file pdf.SaveAs("outputDate.pdf"); } // Compare two dates using CompareTo method public static string CompareDatesWithCompare(DateTime date1, DateTime date2) { int result = date1.CompareTo(date2); string resultString; if (result < 0) { resultString = "date1 is earlier than date2."; Console.WriteLine(resultString); } else if (result > 0) { resultString = "date1 is later than date2."; Console.WriteLine(resultString); } else { resultString = "date1 is equal to date2."; Console.WriteLine(resultString); } return resultString; } // Compare two dates using basic comparison operators public static string CompareDates(DateTime date1, DateTime date2) { string result; if (CheckLessor(date1, date2)) { result = "date1 is earlier than date2."; Console.WriteLine(result); } else if (CheckGreater(date1, date2)) { result = "date1 is later than date2."; Console.WriteLine(result); } else { result = "date1 is equal to date2."; Console.WriteLine(result); } return result; } // Helper method to check if the first date is greater than the second date public static bool CheckGreater(DateTime date1, DateTime date2) { return date1 > date2; } // Helper method to check if the first date is less than the second date public static bool CheckLessor(DateTime date1, DateTime date2) { return date1 < date2; } } $vbLabelText $csharpLabel 以下の出力は、DateTime オブジェクトを使用して生成された PDF を示します。 IronPDF トライアル ライセンス IronPDF は、完全な機能を利用するためにトライアル ライセンスが必要です。 ライセンスキーを生成するためにメール ID を提供すると、メールでライセンスキーが送信されます。 "IronPDF.LicenseKey": "<Your Key>" ライセンスキーを AppSettings.json ファイルに配置します。 結論 C# の DateTime オブジェクトは、.NET アプリケーションで日付と時刻を扱うための強力な方法を提供します。 それらは、日付と時刻の作成、操作、フォーマット、比較の幅広い機能を提供します。 C# アプリケーションで信頼性の高い正確な日付と時刻の機能を構築するためには、DateTime オブジェクトを効果的に使用する方法を理解することが重要です。 開発者は DateTime オブジェクトの能力を活用することで、特定の要件やシナリオにかかわらず、それらの日付と時刻を正確に扱うことができるアプリケーションを保証します。 期間を計算したり、タスクをスケジュールしたり、ユーザーに日付と時刻を表示したりする場合、DateTimeオブジェクトは日付と時間管理に関連するC#プログラミングの多くの側面で重要な役割を果たします。 よくある質問 C# の DateTime オブジェクトは何に使用されますか? C# の DateTime オブジェクトは、.NET フレームワークアプリケーション内で日付と時刻を処理するために使用されます。日付と時刻の操作、フォーマット、および比較の機能を提供し、時間データを扱うアプリケーションにとって不可欠です。 C# で特定の日付の DateTime オブジェクトをどのように作成しますか? C# で特定の日付の DateTime オブジェクトを作成するには、パラメータを使用したコンストラクタを使用します。例えば、DateTime specificDate = new DateTime(2023, 12, 31); は 2023年12月31日の DateTime オブジェクトを作成します。 C# で DateTime オブジェクトを文字列としてどのようにフォーマットしますか? C# では、フォーマット指定子を使用して DateTime オブジェクトを文字列としてフォーマットできます。例えば、dateTime.ToString("yyyy-MM-dd") は日付を '2023-12-31' としてフォーマットします。 C# で DateTime を使用してローカル時間を UTC にどのように変換しますか? C# では、DateTime オブジェクトで ToUniversalTime() メソッドを使用してローカル時間を UTC に変換できます。これは、異なるタイムゾーン間で日時データを標準化するのに役立ちます。 C# で DateTime オブジェクトを比較するために利用可能なメソッドは何ですか? C# では、<、>、<=、>=、==、!= などの演算子を使用して DateTime オブジェクトを比較できます。さらに、CompareTo() メソッドにより、2つの DateTime インスタンスの相対的な順序を判断することができます。 C# で DateTime オブジェクトを使用して PDF ドキュメントをどのように生成しますか? IronPDF を使用すると、C# で DateTime 情報を含む PDF ドキュメントを作成できます。フォーマットされた DateTime 文字列を PDF コンテンツに挿入して、動的な日時データを表示できます。 C# で DateTime を使用してサマータイムの変更をどのように処理しますか? サマータイムは、DateTime オブジェクトを UTC に変換してから ToLocalTime() を使用し、タイムゾーンの調整を考慮して正確な時間表現を確保することで管理できます。 C# アプリケーションで DateTime.UtcNow プロパティが重要なのはなぜですか? DateTime.UtcNow プロパティは現在の UTC 日付と時刻を提供し、ロギングやデータ同期のために一貫性がありタイムゾーンに依存しない時間基準が必要なアプリケーションにとって不可欠です。 C# で DateTime オブジェクトをカスタム形式でフォーマットすることはできますか? はい、C# では ToString() メソッドにフォーマット文字列を提供することで、DateTime オブジェクトをカスタム形式でフォーマットできます。これにより、任意の形式で日付と時刻を表示できます。 C# で DateTime オブジェクトを文字列形式に変換することの重要性は何ですか? C# で DateTime オブジェクトを文字列形式に変換することは、ユーザーインターフェース、レポート、ログで日時情報を表示するために重要です。この変換により、データが読みやすく一貫した形で提示されます。 Jacob Mellor 今すぐエンジニアリングチームとチャット 最高技術責任者(CTO) Jacob Mellorは、Iron Softwareの最高技術責任者であり、C# PDF技術の開拓者としてその先進的な役割を担っています。Iron Softwareのコアコードベースのオリジナルデベロッパーである彼は、創業時から製品のアーキテクチャを形作り、CEOのCameron Rimingtonと協力してNASA、Tesla、全世界の政府機関を含む50人以上の会社に成長させました。Jacobは、1998年から2001年にかけてマンチェスター大学で土木工学の第一級優等学士号(BEng)を取得しました。1999年にロンドンで最初のソフトウェアビジネスを立ち上げ、2005年には最初の.NETコンポーネントを作成し、Microsoftエコシステムにおける複雑な問題の解決を専門にしました。彼の旗艦製品であるIronPDFとIronSuite .NETライブラリは、全世界で3000万以上のNuGetインストールを達成しており、彼の基本コードが世界中で使用されている開発者ツールを支えています。商業的な経験を25年間積み、コードを書くことを41年間続けるJacobは、企業向けのC#、Java、およびPython PDF技術の革新を推進し続け、次世代の技術リーダーを指導しています。 関連する記事 更新日 12月 11, 2025 CLIの簡素化と.NETの橋渡し:Curl DotNetとIronPDFを使う Jacob Mellorは、.NETエコシステムにcURLの親しみやすさをもたらすために作成されたライブラリ、CurlDotNetでこのギャップを埋めました。 詳しく読む 更新日 12月 20, 2025 RandomNumberGenerator C# RandomNumberGenerator C#クラスを使用すると、PDF生成および編集プロジェクトを次のレベルに引き上げることができます 詳しく読む 更新日 12月 20, 2025 C# String Equals(開発者向けの仕組み) 強力なPDFライブラリであるIronPDFと組み合わせることで、switchパターンマッチングは、ドキュメント処理のためのよりスマートでクリーンなロジックを構築できます 詳しく読む C# Data Structures(開発者向けの動作方法)C# Tryparse(開発者向けの動...
更新日 12月 11, 2025 CLIの簡素化と.NETの橋渡し:Curl DotNetとIronPDFを使う Jacob Mellorは、.NETエコシステムにcURLの親しみやすさをもたらすために作成されたライブラリ、CurlDotNetでこのギャップを埋めました。 詳しく読む
更新日 12月 20, 2025 RandomNumberGenerator C# RandomNumberGenerator C#クラスを使用すると、PDF生成および編集プロジェクトを次のレベルに引き上げることができます 詳しく読む
更新日 12月 20, 2025 C# String Equals(開発者向けの仕組み) 強力なPDFライブラリであるIronPDFと組み合わせることで、switchパターンマッチングは、ドキュメント処理のためのよりスマートでクリーンなロジックを構築できます 詳しく読む