DateTime 構造体
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
特定の時点を表します。通常、日時形式で表されます。
public value class DateTime : IComparable, IComparable<DateTime>, IConvertible, IEquatable<DateTime>, IFormattable
public value class DateTime : IComparable, IComparable<DateTime>, IConvertible, IEquatable<DateTime>, IFormattable, System::Runtime::Serialization::ISerializable
public value class DateTime : IComparable, IComparable<DateTime>, IConvertible, IEquatable<DateTime>, ISpanFormattable, System::Runtime::Serialization::ISerializable
public value class DateTime : IComparable, IComparable<DateTime>, IConvertible, IEquatable<DateTime>, IParsable<DateTime>, ISpanFormattable, ISpanParsable<DateTime>, System::Runtime::Serialization::ISerializable
public value class DateTime : IComparable, IComparable<DateTime>, IConvertible, IEquatable<DateTime>, IParsable<DateTime>, ISpanFormattable, ISpanParsable<DateTime>, IUtf8SpanFormattable, System::Runtime::Serialization::ISerializable
public value class DateTime : IComparable, IConvertible, IFormattable
public value class DateTime : IComparable, IComparable<DateTime>, IEquatable<DateTime>, IFormattable
public struct DateTime : IComparable, IComparable<DateTime>, IConvertible, IEquatable<DateTime>, IFormattable
public readonly struct DateTime : IComparable, IComparable<DateTime>, IConvertible, IEquatable<DateTime>, IFormattable, System.Runtime.Serialization.ISerializable
public readonly struct DateTime : IComparable, IComparable<DateTime>, IConvertible, IEquatable<DateTime>, ISpanFormattable, System.Runtime.Serialization.ISerializable
public readonly struct DateTime : IComparable, IComparable<DateTime>, IConvertible, IEquatable<DateTime>, IParsable<DateTime>, ISpanFormattable, ISpanParsable<DateTime>, System.Runtime.Serialization.ISerializable
public readonly struct DateTime : IComparable, IComparable<DateTime>, IConvertible, IEquatable<DateTime>, IParsable<DateTime>, ISpanFormattable, ISpanParsable<DateTime>, IUtf8SpanFormattable, System.Runtime.Serialization.ISerializable
public struct DateTime : IComparable, IComparable<DateTime>, IConvertible, IEquatable<DateTime>, IFormattable, System.Runtime.Serialization.ISerializable
[System.Serializable]
public struct DateTime : IComparable, IConvertible, IFormattable
[System.Serializable]
public struct DateTime : IComparable, IComparable<DateTime>, IConvertible, IEquatable<DateTime>, IFormattable, System.Runtime.Serialization.ISerializable
public struct DateTime : IComparable, IComparable<DateTime>, IEquatable<DateTime>, IFormattable
type DateTime = struct
interface IConvertible
interface IFormattable
type DateTime = struct
interface IConvertible
interface IFormattable
interface ISerializable
type DateTime = struct
interface IConvertible
interface ISpanFormattable
interface IFormattable
interface ISerializable
type DateTime = struct
interface IConvertible
interface IFormattable
interface IParsable<DateTime>
interface ISpanFormattable
interface ISpanParsable<DateTime>
interface ISerializable
type DateTime = struct
interface IConvertible
interface IFormattable
interface IParsable<DateTime>
interface ISpanFormattable
interface ISpanParsable<DateTime>
interface ISerializable
interface IUtf8SpanFormattable
[<System.Serializable>]
type DateTime = struct
interface IFormattable
interface IConvertible
[<System.Serializable>]
type DateTime = struct
interface IFormattable
interface IConvertible
interface ISerializable
type DateTime = struct
interface IFormattable
Public Structure DateTime
Implements IComparable, IComparable(Of DateTime), IConvertible, IEquatable(Of DateTime), IFormattable
Public Structure DateTime
Implements IComparable, IComparable(Of DateTime), IConvertible, IEquatable(Of DateTime), IFormattable, ISerializable
Public Structure DateTime
Implements IComparable, IComparable(Of DateTime), IConvertible, IEquatable(Of DateTime), ISerializable, ISpanFormattable
Public Structure DateTime
Implements IComparable, IComparable(Of DateTime), IConvertible, IEquatable(Of DateTime), IParsable(Of DateTime), ISerializable, ISpanFormattable, ISpanParsable(Of DateTime)
Public Structure DateTime
Implements IComparable, IComparable(Of DateTime), IConvertible, IEquatable(Of DateTime), IParsable(Of DateTime), ISerializable, ISpanFormattable, ISpanParsable(Of DateTime), IUtf8SpanFormattable
Public Structure DateTime
Implements IComparable, IConvertible, IFormattable
Public Structure DateTime
Implements IComparable, IComparable(Of DateTime), IEquatable(Of DateTime), IFormattable
- 継承
- 属性
- 実装
注釈
重要
和暦の時代 (年号) は天皇の代に基づいているため、変更されることが予想されます。 たとえば、JapaneseCalendar と JapaneseLunisolarCalendar において、2019 年 5 月 1 日から令和時代が始まることになりました。 このような時代 (年号) の変更は、これらのカレンダーを使用するすべてのアプリケーションに影響します。 詳細と、アプリケーションが影響を受けるかどうかを判断するには、「 .NET での日本語カレンダーでの新しい時代 (年号の処理)」を参照してください。 Windows システムでアプリケーションをテストして時代 (年号) の変更に対する準備を確認する方法については、「 日本の時代 (年号) の変更に備える」を参照してください。 複数の時代 (年号) を含むカレンダーをサポートする .NET の機能と、複数の時代 (年号) をサポートするカレンダーを使用する場合のベスト プラクティスについては、「 年号の使用」を参照してください。
コード例へのクイック リンク
注意
この記事にある C# の例の一部は、Try.NET インライン コード ランナーとプレイグラウンドで実行されます。 [実行] ボタンを選択すると、対話型ウィンドウで例が実行されます。 コードを実行したら、コードを変更し、 [実行] をもう一度選択して変更後のコードを実行できます。 変更後のコードが対話型ウィンドウで実行されるか、コンパイルできなかった場合、対話型ウィンドウにすべての C# コンパイラ エラー メッセージが表示されます。
Try.NET インライン コード ランナーとプレイグラウンドのローカル タイム ゾーンは、協定世界時 (UTC) です。 これは、DateTime、DateTimeOffset、TimeZoneInfo 型とそのメンバーについて説明する例の、動作と出力に影響を与える可能性があります。
この記事には、 型を使用するいくつかの例が DateTime
含まれています。
初期化の例
オブジェクトを文字列として書式設定するDateTime
- 既定の日時形式を使用する
- 特定のカルチャを使用して日付と時刻を書式設定する
- 標準書式指定文字列またはカスタム書式指定文字列を使用して日付時刻を書式設定する
- 書式指定文字列と特定のカルチャの両方を指定する
- Web サービスの ISO 8601 標準を使用して日付時刻を書式設定する
オブジェクトとしての DateTime
文字列の解析
- または
TryParse
を使用してParse
文字列を日付と時刻に変換する - または
TryParseExact
を使用してParseExact
、文字列を既知の形式で変換します - ISO 8601 文字列表現から日付と時刻への変換
DateTime
解像 度
カルチャとカレンダー
- カルチャ固有のカレンダーを使用して日付と時刻の値を表示する
- カルチャ固有のカレンダーに従って文字列を解析する
- 特定のカルチャの予定表から日付と時刻を初期化する
- 特定のカルチャの予定表を使用して日付と時刻のプロパティにアクセスする
- カルチャ固有のカレンダーを使用して年の週を取得する
永続化
- 日付と時刻の値を文字列としてローカル タイム ゾーンに保持する
- カルチャと時刻の不変形式での文字列としての日付と時刻の値の保持
- 日付と時刻の値を整数として保持する
- を使用して日付と時刻の値を保持する
XmlSerializer
解説トピックへのクイック リンク
このセクションには、構造体の多くの一般的な用途に関するトピックが DateTime
含まれています。
- オブジェクトを初期化する
DateTime
- DateTime 値とその文字列表現
- 文字列から DateTime 値を解析する
- DateTime 値
- DateTime 操作
- DateTime resolution
- DateTime の値とカレンダー
- DateTime 値を永続化する
- DateTime と TimeSpan
- 許容範囲内の等値を比較する
- COM 相互運用に関する考慮事項
値型は DateTime 、グレゴリオ暦の 9999 年 12 月 31 日午後 11 時 59 分 59 分 59 秒までの日付と時刻を表します。
時間値は、ティックと呼ばれる 100 ナノ秒単位で測定されます。 特定の日付は、カレンダーの 0001 年 1 月 1 日午前 0 時 00 GregorianCalendar 分からのティック数です。 この数値は、うるう秒で加算されるティックを除外します。 たとえば、ティック値 31241376000000000L は、0100 年 1 月 1 日の金曜日の午前 12 時 00 分 00 分の日付を表します。 DateTime値は、常に明示的または既定のカレンダーのコンテキストで表されます。
Note
分や秒などの他の時間間隔に変換するティック値を使用している場合は、、または 定数をTimeSpan.TicksPerDayTimeSpan.TicksPerSecondTimeSpan.TicksPerHourTimeSpan.TicksPerMinute使用して変換を実行する必要があります。TimeSpan.TicksPerMillisecond たとえば、指定したティック数で表される秒数を値のDateTimeコンポーネントにSecond追加するには、 式 dateValue.Second + nTicks/Timespan.TicksPerSecond
を使用します。
この記事の一連の例全体のソースは、 GitHub の docs リポジトリから Visual Basic、 F#、または C# のいずれかで確認できます。
注意
特定のタイム ゾーンで日付と時刻の DateTime 値を操作するための構造体の代わりに、 構造体があります DateTimeOffset 。 構造体は DateTimeOffset 、日付と時刻の情報をプライベート フィールドに格納し、その日付と時刻がプライベート DateTimeInt16 フィールドの UTC と異なる分数を格納します。 これにより、値に DateTimeOffset 特定のタイム ゾーンの時刻を反映させることができますが DateTime 、値は UTC とローカル タイム ゾーンの時刻のみを明確に反映できます。 日付と時刻の値を操作するときに構造体または構造体を使用DateTimeするタイミングについては、「DateTime、DateTimeOffset、TimeSpan、TimeZoneInfo の選択」を参照してください。DateTimeOffset
DateTime オブジェクトを初期化する
初期値は、さまざまな方法で新しい DateTime
値に割り当てることができます。
- 値の引数を指定するか、暗黙的なパラメーターなしのコンストラクターを使用するコンストラクターを呼び出します。
DateTime
プロパティまたはメソッドの戻り値に を割り当てる。DateTime
文字列表現からの値の解析。- Visual Basic 固有の言語機能を使用して をインスタンス化する
DateTime
。
次のコード スニペットは、それぞれの例を示しています。
コンストラクターを呼び出す
日付と時刻の値 ( DateTime 年、月、日、ティック数など) の要素を指定するコンストラクターのオーバーロードのいずれかを呼び出します。 次のコードでは、年、月、日、時、分、秒を指定するコンストラクターを使用して DateTime 、特定の日付を作成します。
Dim date1 As New Date(2008, 5, 1, 8, 30, 52)
var date1 = new DateTime(2008, 5, 1, 8, 30, 52);
Console.WriteLine(date1);
let date1 = DateTime(2008, 5, 1, 8, 30, 52)
printfn $"{date1}"
を既定値に DateTime
初期化する場合は、構造体の DateTime
暗黙的なパラメーターなしのコンストラクターを呼び出します。 (値型の暗黙的なパラメーターなしのコンストラクターの詳細については、「値の 型」を参照してください)。一部のコンパイラでは、値を DateTime 明示的に割り当てずに値を宣言することもできます。 明示的な初期化を行わずに値を作成すると、既定値にもなります。 次の例は、 DateTime C# と Visual Basic の暗黙的なパラメーターなしのコンストラクターと、Visual Basic での代入なしの宣言を DateTime 示しています。
Dim dat1 As DateTime
' The following method call displays 1/1/0001 12:00:00 AM.
Console.WriteLine(dat1.ToString(System.Globalization.CultureInfo.InvariantCulture))
' The following method call displays True.
Console.WriteLine(dat1.Equals(Date.MinValue))
Dim dat2 As New DateTime()
' The following method call displays 1/1/0001 12:00:00 AM.
Console.WriteLine(dat2.ToString(System.Globalization.CultureInfo.InvariantCulture))
' The following method call displays True.
Console.WriteLine(dat2.Equals(Date.MinValue))
var dat1 = new DateTime();
// The following method call displays 1/1/0001 12:00:00 AM.
Console.WriteLine(dat1.ToString(System.Globalization.CultureInfo.InvariantCulture));
// The following method call displays True.
Console.WriteLine(dat1.Equals(DateTime.MinValue));
let dat1 = DateTime()
// The following method call displays 1/1/0001 12:00:00 AM.
printfn $"{dat1.ToString System.Globalization.CultureInfo.InvariantCulture}"
// The following method call displays True.
printfn $"{dat1.Equals DateTime.MinValue}"
計算値を割り当てる
プロパティまたはメソッドによって返される日付と時刻の値をオブジェクトに割り当てることができます DateTime 。 次の例では、現在の日付と時刻、現在の協定世界時 (UTC) の日付と時刻、および現在の日付を 3 つの新しい DateTime 変数に割り当てます。
Dim date1 As Date = Date.Now
Dim date2 As Date = Date.UtcNow
Dim date3 As Date = Date.Today
DateTime date1 = DateTime.Now;
DateTime date2 = DateTime.UtcNow;
DateTime date3 = DateTime.Today;
let date1 = DateTime.Now
let date2 = DateTime.UtcNow
let date3 = DateTime.Today
DateTime を表す文字列を解析する
、ParseExact、TryParse、および TryParseExact メソッドはすべてParse、文字列を等価の日付と時刻の値に変換します。 次の例では、 Parse メソッドと ParseExact メソッドを使用して文字列を解析し、それを値に DateTime 変換します。 2 番目の形式では、 ISO 8601 標準でサポートされている形式を使用して、文字列形式で日付と時刻を表します。 この標準的な表現は、多くの場合、Web サービスの日付情報を転送するために使用されます。
Dim dateString As String = "5/1/2008 8:30:52 AM"
Dim date1 As Date = Date.Parse(dateString,
System.Globalization.CultureInfo.InvariantCulture)
Dim iso8601String As String = "20080501T08:30:52Z"
Dim dateISO8602 As Date = DateTime.ParseExact(iso8601String, "yyyyMMddTHH:mm:ssZ",
System.Globalization.CultureInfo.InvariantCulture)
Console.WriteLine(dateISO8602)
var dateString = "5/1/2008 8:30:52 AM";
DateTime date1 = DateTime.Parse(dateString,
System.Globalization.CultureInfo.InvariantCulture);
var iso8601String = "20080501T08:30:52Z";
DateTime dateISO8602 = DateTime.ParseExact(iso8601String, "yyyyMMddTHH:mm:ssZ",
System.Globalization.CultureInfo.InvariantCulture);
let dateString = "5/1/2008 8:30:52 AM"
let date1 = DateTime.Parse(dateString, System.Globalization.CultureInfo.InvariantCulture)
let iso8601String = "20080501T08:30:52Z"
let dateISO8602 = DateTime.ParseExact(iso8601String, "yyyyMMddTHH:mm:ssZ", System.Globalization.CultureInfo.InvariantCulture)
メソッドと TryParseExact メソッドはTryParse、文字列が値のDateTime有効な表現であるかどうかを示し、有効な場合は変換を実行します。
Visual Basic の言語固有の構文
次の Visual Basic ステートメントは、新 DateTime しい値を初期化します。
Dim date1 As Date = #5/1/2008 8:30:52AM#
DateTime 値とその文字列表現
内部的には、すべての DateTime 値は、0001 年 1 月 1 日の午前 0 時 00 分から経過したティック数 (100 ナノ秒間隔の数) として表されます。 実際 DateTime の値は、表示時にその値が表示される方法とは無関係です。 値の外観は、 DateTime 値を文字列表現に変換する書式設定操作の結果です。
日付と時刻の値の外観は、カルチャ、国際標準、アプリケーション要件、個人の好みに依存します。 構造体では DateTime 、 のオーバーロードを使用して日付と時刻の値を柔軟に ToString書式設定できます。 既定 DateTime.ToString() のメソッドは、現在のカルチャの短い日付と長い時間パターンを使用して、日付と時刻の値の文字列表現を返します。 次の例では、既定のメソッドを使用します DateTime.ToString() 。 現在のカルチャの短い日付と長い時間パターンを使用して、日付と時刻が表示されます。 en-US カルチャは、この例が実行されたコンピューター上の現在のカルチャです。
var date1 = new DateTime(2008, 3, 1, 7, 0, 0);
Console.WriteLine(date1.ToString());
// For en-US culture, displays 3/1/2008 7:00:00 AM
let date1 = DateTime(2008, 3, 1, 7, 0, 0)
printfn $"{date1.ToString()}"
// For en-US culture, displays 3/1/2008 7:00:00 AM
Dim date1 As Date = #3/1/2008 7:00AM#
Console.WriteLine(date1.ToString())
' For en-US culture, displays 3/1/2008 7:00:00 AM
サーバーがクライアントとは異なるカルチャにある可能性がある Web シナリオをサポートするために、特定のカルチャの日付を書式設定する必要がある場合があります。 メソッドを使用してカルチャを DateTime.ToString(IFormatProvider) 指定し、特定のカルチャで短い日付と長い時間の表現を作成します。 次の例では、 メソッドを DateTime.ToString(IFormatProvider) 使用して、fr-FR カルチャの短い日付と長い時刻パターンを使用して日付と時刻を表示します。
var date1 = new DateTime(2008, 3, 1, 7, 0, 0);
Console.WriteLine(date1.ToString(System.Globalization.CultureInfo.CreateSpecificCulture("fr-FR")));
// Displays 01/03/2008 07:00:00
let date1 = DateTime(2008, 3, 1, 7, 0, 0)
printfn $"""{date1.ToString(System.Globalization.CultureInfo.CreateSpecificCulture "fr-FR")}"""
// Displays 01/03/2008 07:00:00
Dim date1 As Date = #3/1/2008 7:00AM#
Console.WriteLine(date1.ToString(System.Globalization.CultureInfo.CreateSpecificCulture("fr-FR")))
' Displays 01/03/2008 07:00:00
他のアプリケーションでは、日付の異なる文字列表現が必要になる場合があります。 メソッドは DateTime.ToString(String) 、現在のカルチャの書式設定規則を使用して、標準またはカスタム書式指定子によって定義された文字列表現を返します。 次の例では、 メソッドを DateTime.ToString(String) 使用して、en-US カルチャの完全な日付と時刻のパターン (例が実行されたコンピューター上の現在のカルチャ) を表示します。
var date1 = new DateTime(2008, 3, 1, 7, 0, 0);
Console.WriteLine(date1.ToString("F"));
// Displays Saturday, March 01, 2008 7:00:00 AM
let date1 = DateTime(2008, 3, 1, 7, 0, 0)
printfn $"""{date1.ToString "F"}"""
// Displays Saturday, March 01, 2008 7:00:00 AM
Dim date1 As Date = #3/1/2008 7:00AM#
Console.WriteLine(date1.ToString("F"))
' Displays Saturday, March 01, 2008 7:00:00 AM
最後に、 メソッドを使用してカルチャと形式の両方を DateTime.ToString(String, IFormatProvider) 指定できます。 次の例では、 メソッドを DateTime.ToString(String, IFormatProvider) 使用して、fr-FR カルチャの完全な日付と時刻のパターンを表示します。
var date1 = new DateTime(2008, 3, 1, 7, 0, 0);
Console.WriteLine(date1.ToString("F", new System.Globalization.CultureInfo("fr-FR")));
// Displays samedi 1 mars 2008 07:00:00
let date1 = DateTime(2008, 3, 1, 7, 0, 0)
printfn $"""{date1.ToString("F", new System.Globalization.CultureInfo "fr-FR")}"""
// Displays samedi 1 mars 2008 07:00:00
Dim date1 As Date = #3/1/2008 7:00AM#
Console.WriteLine(date1.ToString("F", New System.Globalization.CultureInfo("fr-FR")))
' Displays samedi 1 mars 2008 07:00:00
オーバーロードは DateTime.ToString(String) 、カスタム書式指定文字列と共に使用して、他の形式を指定することもできます。 次の例は、Web サービスでよく使用される ISO 8601 標準形式を使用して文字列を書式設定する方法を示しています。 Iso 8601 形式には、対応する標準書式指定文字列がありません。
var date1 = new DateTime(2008, 3, 1, 7, 0, 0, DateTimeKind.Utc);
Console.WriteLine(date1.ToString("yyyy-MM-ddTHH:mm:sszzz", System.Globalization.CultureInfo.InvariantCulture));
// Displays 2008-03-01T07:00:00+00:00
let date1 = DateTime(2008, 3, 1, 7, 0, 0, DateTimeKind.Utc)
printfn $"""{date1.ToString("yyyy-MM-ddTHH:mm:sszzz", System.Globalization.CultureInfo.InvariantCulture)}"""
// Displays 2008-03-01T07:00:00+00:00
Dim date1 As DateTime = New DateTime(2008, 3, 1, 7, 0, 0, DateTimeKind.Utc)
Console.WriteLine(date1.ToString("yyyy-MM-ddTHH:mm:sszzz", System.Globalization.CultureInfo.InvariantCulture))
' Displays 2008-03-01T07:00:00+00:00
値の DateTime 書式設定の詳細については、「 標準の日付と時刻の書式指定文字列 」および「 ユーザー設定の日付と時刻の書式指定文字列」を参照してください。
文字列から DateTime 値を解析する
解析により、日付と時刻の文字列表現が値に DateTime 変換されます。 通常、日付と時刻の文字列には、アプリケーションで 2 つの異なる使用法があります。
日付と時刻はさまざまな形式を取り、現在のカルチャまたは特定のカルチャの規則を反映します。 たとえば、アプリケーションを使用すると、現在のカルチャが en-US であるユーザーは、日付値を "12/15/2013" または "December 15, 2013" として入力できます。 これにより、現在のカルチャが en-gb であるユーザーは、日付値を "15/12/2013" または "2013 年 12 月 15 日" として入力できます。
日付と時刻は、定義済みの形式で表されます。 たとえば、アプリケーションは、アプリが実行されているカルチャとは無関係に、日付を "20130103" としてシリアル化します。 アプリケーションでは、現在のカルチャの短い日付形式で日付を入力する必要があります。
または TryParse メソッドをParse使用して、カルチャで使用される一般的な日付と時刻の形式の 1 つから値に文字列をDateTime変換します。 次の例では、 を使用 TryParse して、異なるカルチャ固有の形式の日付文字列を値に変換する方法を DateTime 示します。 現在のカルチャを英語 (英国) に変更し、 メソッドを GetDateTimeFormats() 呼び出して日付と時刻の文字列の配列を生成します。 次に、配列内の各要素を メソッドに TryParse 渡します。 この例の出力は、解析メソッドがカルチャ固有の各日付と時刻の文字列を正常に変換できることを示しています。
System.Threading.Thread.CurrentThread.CurrentCulture =
System.Globalization.CultureInfo.CreateSpecificCulture("en-GB");
var date1 = new DateTime(2013, 6, 1, 12, 32, 30);
var badFormats = new List<String>();
Console.WriteLine($"{"Date String",-37} {"Date",-19}\n");
foreach (var dateString in date1.GetDateTimeFormats())
{
DateTime parsedDate;
if (DateTime.TryParse(dateString, out parsedDate))
Console.WriteLine($"{dateString,-37} {DateTime.Parse(dateString),-19}");
else
badFormats.Add(dateString);
}
// Display strings that could not be parsed.
if (badFormats.Count > 0)
{
Console.WriteLine("\nStrings that could not be parsed: ");
foreach (var badFormat in badFormats)
Console.WriteLine($" {badFormat}");
}
// Press "Run" to see the output.
System.Threading.Thread.CurrentThread.CurrentCulture <-
System.Globalization.CultureInfo.CreateSpecificCulture "en-GB"
let date1 = DateTime(2013, 6, 1, 12, 32, 30)
let badFormats = ResizeArray<String>()
printfn "%-37s %-19s\n" "Date String" "Date"
for dateString in date1.GetDateTimeFormats() do
match DateTime.TryParse dateString with
| true, parsedDate ->
printfn $"%-37s{dateString} %-19O{parsedDate}\n"
| _ ->
badFormats.Add dateString
// Display strings that could not be parsed.
if badFormats.Count > 0 then
printfn "\nStrings that could not be parsed: "
for badFormat in badFormats do
printfn $" {badFormat}"
// Press "Run" to see the output.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB")
Dim date1 As New DateTime(2013, 6, 1, 12, 32, 30)
Dim badFormats As New List(Of String)
Console.WriteLine($"{"Date String",-37} {"Date",-19}")
Console.WriteLine()
For Each dateString As String In date1.GetDateTimeFormats()
Dim parsedDate As DateTime
If DateTime.TryParse(dateString, parsedDate) Then
Console.WriteLine($"{dateString,-37} {DateTime.Parse(dateString),-19:g}")
Else
badFormats.Add(dateString)
End If
Next
' Display strings that could not be parsed.
If badFormats.Count > 0 Then
Console.WriteLine()
Console.WriteLine("Strings that could not be parsed: ")
For Each badFormat In badFormats
Console.WriteLine($" {badFormat}")
Next
End If
' The example displays the following output:
' Date String Date
'
' 01/06/2013 01/06/2013 00:00:00
' 01/06/13 01/06/2013 00:00:00
' 1/6/13 01/06/2013 00:00:00
' 1.6.13 01/06/2013 00:00:00
' 2013-06-01 01/06/2013 00:00:00
' 01 June 2013 01/06/2013 00:00:00
' 1 June 2013 01/06/2013 00:00:00
' 01 June 2013 12:32 01/06/2013 12:32:00
' 01 June 2013 12:32 01/06/2013 12:32:00
' 01 June 2013 12:32 PM 01/06/2013 12:32:00
' 01 June 2013 12:32 PM 01/06/2013 12:32:00
' 1 June 2013 12:32 01/06/2013 12:32:00
' 1 June 2013 12:32 01/06/2013 12:32:00
' 1 June 2013 12:32 PM 01/06/2013 12:32:00
' 1 June 2013 12:32 PM 01/06/2013 12:32:00
' 01 June 2013 12:32:30 01/06/2013 12:32:30
' 01 June 2013 12:32:30 01/06/2013 12:32:30
' 01 June 2013 12:32:30 PM 01/06/2013 12:32:30
' 01 June 2013 12:32:30 PM 01/06/2013 12:32:30
' 1 June 2013 12:32:30 01/06/2013 12:32:30
' 1 June 2013 12:32:30 01/06/2013 12:32:30
' 1 June 2013 12:32:30 PM 01/06/2013 12:32:30
' 1 June 2013 12:32:30 PM 01/06/2013 12:32:30
' 01/06/2013 12:32 01/06/2013 12:32:00
' 01/06/2013 12:32 01/06/2013 12:32:00
' 01/06/2013 12:32 PM 01/06/2013 12:32:00
' 01/06/2013 12:32 PM 01/06/2013 12:32:00
' 01/06/13 12:32 01/06/2013 12:32:00
' 01/06/13 12:32 01/06/2013 12:32:00
' 01/06/13 12:32 PM 01/06/2013 12:32:00
' 01/06/13 12:32 PM 01/06/2013 12:32:00
' 1/6/13 12:32 01/06/2013 12:32:00
' 1/6/13 12:32 01/06/2013 12:32:00
' 1/6/13 12:32 PM 01/06/2013 12:32:00
' 1/6/13 12:32 PM 01/06/2013 12:32:00
' 1.6.13 12:32 01/06/2013 12:32:00
' 1.6.13 12:32 01/06/2013 12:32:00
' 1.6.13 12:32 PM 01/06/2013 12:32:00
' 1.6.13 12:32 PM 01/06/2013 12:32:00
' 2013-06-01 12:32 01/06/2013 12:32:00
' 2013-06-01 12:32 01/06/2013 12:32:00
' 2013-06-01 12:32 PM 01/06/2013 12:32:00
' 2013-06-01 12:32 PM 01/06/2013 12:32:00
' 01/06/2013 12:32:30 01/06/2013 12:32:30
' 01/06/2013 12:32:30 01/06/2013 12:32:30
' 01/06/2013 12:32:30 PM 01/06/2013 12:32:30
' 01/06/2013 12:32:30 PM 01/06/2013 12:32:30
' 01/06/13 12:32:30 01/06/2013 12:32:30
' 01/06/13 12:32:30 01/06/2013 12:32:30
' 01/06/13 12:32:30 PM 01/06/2013 12:32:30
' 01/06/13 12:32:30 PM 01/06/2013 12:32:30
' 1/6/13 12:32:30 01/06/2013 12:32:30
' 1/6/13 12:32:30 01/06/2013 12:32:30
' 1/6/13 12:32:30 PM 01/06/2013 12:32:30
' 1/6/13 12:32:30 PM 01/06/2013 12:32:30
' 1.6.13 12:32:30 01/06/2013 12:32:30
' 1.6.13 12:32:30 01/06/2013 12:32:30
' 1.6.13 12:32:30 PM 01/06/2013 12:32:30
' 1.6.13 12:32:30 PM 01/06/2013 12:32:30
' 2013-06-01 12:32:30 01/06/2013 12:32:30
' 2013-06-01 12:32:30 01/06/2013 12:32:30
' 2013-06-01 12:32:30 PM 01/06/2013 12:32:30
' 2013-06-01 12:32:30 PM 01/06/2013 12:32:30
' 01 June 01/06/2013 00:00:00
' 01 June 01/06/2013 00:00:00
' 2013-06-01T12:32:30.0000000 01/06/2013 12:32:30
' 2013-06-01T12:32:30.0000000 01/06/2013 12:32:30
' Sat, 01 Jun 2013 12:32:30 GMT 01/06/2013 05:32:30
' Sat, 01 Jun 2013 12:32:30 GMT 01/06/2013 05:32:30
' 2013-06-01T12:32:30 01/06/2013 12:32:30
' 12:32 22/04/2013 12:32:00
' 12:32 22/04/2013 12:32:00
' 12:32 PM 22/04/2013 12:32:00
' 12:32 PM 22/04/2013 12:32:00
' 12:32:30 22/04/2013 12:32:30
' 12:32:30 22/04/2013 12:32:30
' 12:32:30 PM 22/04/2013 12:32:30
' 12:32:30 PM 22/04/2013 12:32:30
' 2013-06-01 12:32:30Z 01/06/2013 05:32:30
' 01 June 2013 19:32:30 01/06/2013 19:32:30
' 01 June 2013 19:32:30 01/06/2013 19:32:30
' 01 June 2013 07:32:30 PM 01/06/2013 19:32:30
' 01 June 2013 7:32:30 PM 01/06/2013 19:32:30
' 1 June 2013 19:32:30 01/06/2013 19:32:30
' 1 June 2013 19:32:30 01/06/2013 19:32:30
' 1 June 2013 07:32:30 PM 01/06/2013 19:32:30
' 1 June 2013 7:32:30 PM 01/06/2013 19:32:30
' June 2013 01/06/2013 00:00:00
' June 2013 01/06/2013 00:00:00
メソッドと メソッドをParseExact使用して、特定の形式または書式に一致する必要がある文字列をDateTime値に変換TryParseExactします。 1 つ以上の日付と時刻の書式指定文字列を解析メソッドのパラメーターとして指定します。 次の例では、 メソッドを TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTime) 使用して、"yyyyMMdd" 形式または "HHmmss" 形式の文字列を値に DateTime 変換します。
string[] formats = { "yyyyMMdd", "HHmmss" };
string[] dateStrings = { "20130816", "20131608", " 20130816 ",
"115216", "521116", " 115216 " };
DateTime parsedDate;
foreach (var dateString in dateStrings)
{
if (DateTime.TryParseExact(dateString, formats, null,
System.Globalization.DateTimeStyles.AllowWhiteSpaces |
System.Globalization.DateTimeStyles.AdjustToUniversal,
out parsedDate))
Console.WriteLine($"{dateString} --> {parsedDate:g}");
else
Console.WriteLine($"Cannot convert {dateString}");
}
// The example displays the following output:
// 20130816 --> 8/16/2013 12:00 AM
// Cannot convert 20131608
// 20130816 --> 8/16/2013 12:00 AM
// 115216 --> 4/22/2013 11:52 AM
// Cannot convert 521116
// 115216 --> 4/22/2013 11:52 AM
let formats = [| "yyyyMMdd"; "HHmmss" |]
let dateStrings =
[ "20130816"; "20131608"; " 20130816 "
"115216"; "521116"; " 115216 " ]
for dateString in dateStrings do
match DateTime.TryParseExact(dateString, formats, null,
System.Globalization.DateTimeStyles.AllowWhiteSpaces |||
System.Globalization.DateTimeStyles.AdjustToUniversal) with
| true, parsedDate ->
printfn $"{dateString} --> {parsedDate:g}"
| _ ->
printfn $"Cannot convert {dateString}"
// The example displays the following output:
// 20130816 --> 8/16/2013 12:00 AM
// Cannot convert 20131608
// 20130816 --> 8/16/2013 12:00 AM
// 115216 --> 4/22/2013 11:52 AM
// Cannot convert 521116
// 115216 --> 4/22/2013 11:52 AM
Dim formats() As String = {"yyyyMMdd", "HHmmss"}
Dim dateStrings() As String = {"20130816", "20131608",
" 20130816 ", "115216",
"521116", " 115216 "}
Dim parsedDate As DateTime
For Each dateString As String In dateStrings
If DateTime.TryParseExact(dateString, formats, Nothing,
DateTimeStyles.AllowWhiteSpaces Or
DateTimeStyles.AdjustToUniversal,
parsedDate) Then
Console.WriteLine($"{dateString} --> {parsedDate:g}")
Else
Console.WriteLine($"Cannot convert {dateString}")
End If
Next
' The example displays the following output:
' 20130816 --> 8/16/2013 12:00 AM
' Cannot convert 20131608
' 20130816 --> 8/16/2013 12:00 AM
' 115216 --> 4/22/2013 11:52 AM
' Cannot convert 521116
' 115216 --> 4/22/2013 11:52 AM
の一般的な用途の ParseExact 1 つは、Web サービスから文字列表現 (通常 は ISO 8601 標準形式) を変換することです。 次のコードは、使用する正しい書式指定文字列を示しています。
var iso8601String = "20080501T08:30:52Z";
DateTime dateISO8602 = DateTime.ParseExact(iso8601String, "yyyyMMddTHH:mm:ssZ",
System.Globalization.CultureInfo.InvariantCulture);
Console.WriteLine($"{iso8601String} --> {dateISO8602:g}");
let iso8601String = "20080501T08:30:52Z"
let dateISO8602 = DateTime.ParseExact(iso8601String, "yyyyMMddTHH:mm:ssZ", System.Globalization.CultureInfo.InvariantCulture)
printfn $"{iso8601String} --> {dateISO8602:g}"
Dim iso8601String As String = "20080501T08:30:52Z"
Dim dateISO8602 As DateTime = DateTime.ParseExact(iso8601String, "yyyyMMddTHH:mm:ssZ", CultureInfo.InvariantCulture)
Console.WriteLine($"{iso8601String} --> {dateISO8602:g}")
文字列を解析できない場合、 Parse メソッドと ParseExact メソッドは例外をスローします。 メソッドと TryParseExact メソッドはTryParse、Boolean変換が成功したか失敗したかを示す値を返します。 パフォーマンスが TryParse 重要なシナリオでは、 メソッドまたは TryParseExact メソッドを使用する必要があります。 日付と時刻の文字列の解析操作は、エラー率が高くなる傾向があり、例外処理はコストがかかります。 文字列がユーザーによって入力された場合、または不明なソースから取得された場合は、これらのメソッドを使用します。
日付と時刻の値の解析の詳細については、「 日付と時刻の文字列の解析」を参照してください。
DateTime 値
型の時刻値 DateTime の説明は、多くの場合、協定世界時 (UTC) 標準を使用して表されます。 協定世界時は、グリニッジ標準時 (GMT) の国際的に認められた名前です。 協定世界時は、UTC の原点である経度 0 度で測定された時間です。 夏時間は UTC には適用されません。
ローカル時刻は、特定のタイム ゾーンを基準にしています。 タイム ゾーンはタイム ゾーン オフセットに関連付けられます。 タイム ゾーン オフセットは、UTC の起点から時間単位で測定されたタイム ゾーンの変位です。 さらに、ローカル時刻は必要に応じて夏時間の影響を受け、時間間隔の調整を加算または減算します。 現地時刻は、UTC にタイム ゾーン オフセットを追加し、必要に応じて夏時間を調整することによって計算されます。 UTC 起点のタイム ゾーン オフセットは 0 です。
UTC 時刻は、計算、比較、および日付と時刻のファイルへの格納に適しています。 ローカル時刻は、デスクトップ アプリケーションのユーザー インターフェイスでの表示に適しています。 タイム ゾーン対応アプリケーション (多くの Web アプリケーションなど) も、他の多くのタイム ゾーンと連携する必要があります。
オブジェクトの KindDateTime プロパティが の場合、 DateTimeKind.Unspecified表される時刻がローカル時刻、UTC 時刻、またはその他のタイム ゾーンの時刻であるかどうかは指定されません。
DateTime の解決
プロパティは Ticks 、日付と時刻の値を 1 秒の 1,000 万分の 1 の単位で表します。 プロパティは Millisecond 、日付と時刻の値の 1 秒の 1000 分の 1 を返します。 プロパティを DateTime.Now 繰り返し呼び出して経過時間を測定することは、システム クロックによって異なります。 Windows 7 および Windows 8 システムのシステム クロックの解像度は約 15 ミリ秒です。 この解像度は、100 ミリ秒未満の短い時間間隔に影響します。
次の例は、現在の日付と時刻の値がシステム クロックの解像度に依存する方法を示しています。 この例では、外側のループが 20 回繰り返され、内部ループが外側のループを遅延させる役割を果たします。 外側のループ カウンターの値が 10 の場合、 メソッドを Thread.Sleep 呼び出すと 5 ミリ秒の遅延が発生します。 次の例は、 への呼び出し後にのみ、 プロパティが変更して DateTime.Now.Milliseconds
返されるミリ秒数を Thread.Sleep示しています。
string output = "";
for (int ctr = 0; ctr <= 20; ctr++)
{
output += String.Format($"{DateTime.Now.Millisecond}\n");
// Introduce a delay loop.
for (int delay = 0; delay <= 1000; delay++)
{ }
if (ctr == 10)
{
output += "Thread.Sleep called...\n";
System.Threading.Thread.Sleep(5);
}
}
Console.WriteLine(output);
// Press "Run" to see the output.
let mutable output = ""
for i = 0 to 20 do
output <- output + $"{DateTime.Now.Millisecond}\n"
// Introduce a delay loop.
for _ = 0 to 1000 do ()
if i = 10 then
output <- output + "Thread.Sleep called...\n"
System.Threading.Thread.Sleep 5
printfn $"{output}"
// Press "Run" to see the output.
Dim output As String = ""
For ctr As Integer = 0 To 20
output += Date.Now.Millisecond.ToString() + vbCrLf
' Introduce a delay loop.
For delay As Integer = 0 To 1000
Next
If ctr = 10 Then
output += "Thread.Sleep called..." + vbCrLf
Thread.Sleep(5)
End If
Next
Console.WriteLine(output)
' The example displays output like the following:
' 111
' 111
' 111
' 111
' 111
' 111
' 111
' 111
' 111
' 111
' 111
' Thread.Sleep called...
' 143
' 143
' 143
' 143
' 143
' 143
' 143
' 143
' 143
' 143
DateTime 操作
や SubtractなどのAdd構造体を使用したDateTime計算では、構造体の値は変更されません。 代わりに、計算は、計算の結果である値を持つ新しい DateTime 構造体を返します。
タイム ゾーン間の変換操作 (UTC とローカル時刻の間、またはタイム ゾーンとタイム ゾーン間など) では夏時間が考慮されますが、算術演算と比較操作では考慮されません。
構造体自体では DateTime 、あるタイム ゾーンから別のタイム ゾーンへの変換のサポートが制限されています。 メソッドを ToLocalTime 使用して UTC を現地時刻に変換することも、 メソッドを ToUniversalTime 使用して現地時刻から UTC に変換することもできます。 ただし、 クラスでは、タイム ゾーン変換メソッドの完全なセットを TimeZoneInfo 使用できます。 これらのメソッドを使用して、世界のいずれかのタイム ゾーンの時刻を他のタイム ゾーンの時刻に変換します。
オブジェクトの DateTime 計算と比較は、オブジェクトが同じタイム ゾーンの時刻を表す場合にのみ意味があります。 オブジェクトを TimeZoneInfo 使用して値のタイム ゾーンを DateTime 表すことができますが、2 つの値は疎結合されています。 DateTimeオブジェクトには、その日付と時刻の値のタイム ゾーンを表す オブジェクトを返す プロパティがありません。 プロパティは Kind 、 が UTC、現地時刻、または指定されていないかどうかを DateTime
示します。 タイム ゾーン対応アプリケーションでは、オブジェクトが作成されたタイム ゾーンを特定するために、外部メカニズムに依存する DateTime 必要があります。 値と値のタイム ゾーンを表す オブジェクトの DateTime 両方を TimeZoneInfo ラップする構造体を DateTime 使用できます。 計算で UTC を使用する方法と値との DateTime 比較の詳細については、「 日付と時刻を使用した算術演算の実行」を参照してください。
各 DateTime メンバーは、グレゴリオ暦を暗黙的に使用してその操作を実行します。 例外は、カレンダーを暗黙的に指定するメソッドです。 これには、カレンダーを指定するコンストラクターや、 から IFormatProvider派生したパラメーターを持つメソッド (など) が含 System.Globalization.DateTimeFormatInfoまれます。
型のメンバーによる操作では、 DateTime 閏年や 1 か月の日数などの詳細が考慮されます。
DateTime の値と予定表
.NET クラス ライブラリには、多数のカレンダー クラスが含まれており、これらはすべて クラスから Calendar 派生しています。 これらは次のとおりです。
- ChineseLunisolarCalendar クラスです。
- EastAsianLunisolarCalendar クラスです。
- GregorianCalendar クラスです。
- HebrewCalendar クラスです。
- HijriCalendar クラスです。
- JapaneseCalendar クラスです。
- JapaneseLunisolarCalendar クラスです。
- JulianCalendar クラスです。
- KoreanCalendar クラスです。
- KoreanLunisolarCalendar クラスです。
- PersianCalendar クラスです。
- TaiwanCalendar クラスです。
- TaiwanLunisolarCalendar クラスです。
- ThaiBuddhistCalendar クラスです。
- UmAlQuraCalendar クラスです。
重要
和暦の時代 (年号) は天皇の代に基づいているため、変更されることが予想されます。 たとえば、JapaneseCalendar と JapaneseLunisolarCalendar において、2019 年 5 月 1 日から令和時代が始まることになりました。 このような時代 (年号) の変更は、これらのカレンダーを使用するすべてのアプリケーションに影響します。 詳細と、アプリケーションが影響を受けるかどうかを判断するには、「 .NET での日本語カレンダーでの新しい時代 (年号の処理)」を参照してください。 Windows システムでアプリケーションをテストして時代 (年号) の変更に対する準備を確認する方法については、「 日本の時代 (年号) の変更に備える」を参照してください。 複数の時代 (年号) を含むカレンダーをサポートする .NET の機能と、複数の時代 (年号) をサポートするカレンダーを使用する場合のベスト プラクティスについては、「 年号の使用」を参照してください。
各カルチャでは、読み取り専用 CultureInfo.Calendar プロパティで定義された既定の予定表が使用されます。 各カルチャでは、読み取り専用 CultureInfo.OptionalCalendars プロパティで定義された 1 つ以上の予定表をサポートできます。 特定 CultureInfo のオブジェクトで現在使用されているカレンダーは、その DateTimeFormatInfo.Calendar プロパティによって定義されます。 配列内 CultureInfo.OptionalCalendars にあるカレンダーのいずれかである必要があります。
カルチャの現在の予定表は、そのカルチャのすべての書式設定操作で使用されます。 たとえば、タイ仏教文化の既定のカレンダーは、クラスで ThaiBuddhistCalendar 表されるタイ仏教暦です。 タイ仏教の文化を CultureInfo 表すオブジェクトを日付と時刻の書式設定操作で使用する場合、既定では、タイ仏教暦が使用されます。 グレゴリオ暦は、次の例に示すように、カルチャの DateTimeFormatInfo.Calendar プロパティが変更された場合にのみ使用されます。
var thTH = new System.Globalization.CultureInfo("th-TH");
var value = new DateTime(2016, 5, 28);
Console.WriteLine(value.ToString(thTH));
thTH.DateTimeFormat.Calendar = new System.Globalization.GregorianCalendar();
Console.WriteLine(value.ToString(thTH));
// The example displays the following output:
// 28/5/2559 0:00:00
// 28/5/2016 0:00:00
let thTH = System.Globalization.CultureInfo "th-TH"
let value = DateTime(2016, 5, 28)
printfn $"{value.ToString thTH}"
thTH.DateTimeFormat.Calendar <- System.Globalization.GregorianCalendar()
printfn $"{value.ToString thTH}"
// The example displays the following output:
// 28/5/2559 0:00:00
// 28/5/2016 0:00:00
Dim thTH As New CultureInfo("th-TH")
Dim value As New DateTime(2016, 5, 28)
Console.WriteLine(value.ToString(thTH))
thTH.DateTimeFormat.Calendar = New GregorianCalendar()
Console.WriteLine(value.ToString(thTH))
' The example displays the following output:
' 28/5/2559 0:00:00
' 28/5/2016 0:00:00
カルチャの現在のカレンダーは、次の例に示すように、そのカルチャのすべての解析操作でも使用されます。
var thTH = new System.Globalization.CultureInfo("th-TH");
var value = DateTime.Parse("28/05/2559", thTH);
Console.WriteLine(value.ToString(thTH));
thTH.DateTimeFormat.Calendar = new System.Globalization.GregorianCalendar();
Console.WriteLine(value.ToString(thTH));
// The example displays the following output:
// 28/5/2559 0:00:00
// 28/5/2016 0:00:00
let thTH = System.Globalization.CultureInfo "th-TH"
let value = DateTime.Parse("28/05/2559", thTH)
printfn $"{value.ToString thTH}"
thTH.DateTimeFormat.Calendar <- System.Globalization.GregorianCalendar()
printfn $"{value.ToString thTH}"
// The example displays the following output:
// 28/5/2559 0:00:00
// 28/5/2016 0:00:00
Private Sub ThaiBuddhistEraParse()
Dim thTH As New CultureInfo("th-TH")
Dim value As DateTime = DateTime.Parse("28/5/2559", thTH)
Console.WriteLine(value.ToString(thTH))
thTH.DateTimeFormat.Calendar = New GregorianCalendar()
Console.WriteLine(value.ToString(thTH))
' The example displays the following output:
' 28/5/2559 0:00:00
' 28/5/2016 0:00:00
End Sub
パラメーターをDateTime含む calendar
DateTime コンストラクターを呼び出し、そのカレンダーを表すオブジェクトを渡Calendarすことで、特定のカレンダーの日付と時刻の要素 (年、月、日の数) を使用して値をインスタンス化します。 次の例では、カレンダーの日付と時刻の要素を ThaiBuddhistCalendar 使用します。
var thTH = new System.Globalization.CultureInfo("th-TH");
var dat = new DateTime(2559, 5, 28, thTH.DateTimeFormat.Calendar);
Console.WriteLine($"Thai Buddhist era date: {dat.ToString("d", thTH)}");
Console.WriteLine($"Gregorian date: {dat:d}");
// The example displays the following output:
// Thai Buddhist Era Date: 28/5/2559
// Gregorian Date: 28/05/2016
let thTH = System.Globalization.CultureInfo "th-TH"
let dat = DateTime(2559, 5, 28, thTH.DateTimeFormat.Calendar)
printfn $"""Thai Buddhist era date: {dat.ToString("d", thTH)}"""
printfn $"Gregorian date: {dat:d}"
// The example displays the following output:
// Thai Buddhist Era Date: 28/5/2559
// Gregorian Date: 28/05/2016
Dim thTH As New CultureInfo("th-TH")
Dim dat As New DateTime(2559, 5, 28, thTH.DateTimeFormat.Calendar)
Console.WriteLine($"Thai Buddhist Era date: {dat.ToString("d", thTH)}")
Console.WriteLine($"Gregorian date: {dat:d}")
' The example displays the following output:
' Thai Buddhist Era Date: 28/5/2559
' Gregorian Date: 28/05/2016
DateTime パラメーターを含 calendar
まないコンストラクターは、日付と時刻の要素がグレゴリオ暦で単位として表されることを前提としています。
その他 DateTime のすべてのプロパティとメソッドでは、グレゴリオ暦が使用されます。 たとえば、 プロパティは DateTime.Year グレゴリオ暦の年を返し DateTime.IsLeapYear(Int32) 、 メソッドは パラメーターがグレゴリオ暦の年であることを year
前提としています。 グレゴリオ暦を使用する各 DateTime メンバーには、特定のカレンダーを Calendar 使用する クラスの対応するメンバーがあります。 たとえば、 メソッドは Calendar.GetYear 特定のカレンダーで年を返し Calendar.IsLeapYear 、 メソッドはパラメーターを year
特定のカレンダーの年番号として解釈します。 次の例では、 DateTime クラスの メンバーと対応するメンバーの両方を ThaiBuddhistCalendar 使用します。
var thTH = new System.Globalization.CultureInfo("th-TH");
var cal = thTH.DateTimeFormat.Calendar;
var dat = new DateTime(2559, 5, 28, cal);
Console.WriteLine("Using the Thai Buddhist Era calendar:");
Console.WriteLine($"Date: {dat.ToString("d", thTH)}");
Console.WriteLine($"Year: {cal.GetYear(dat)}");
Console.WriteLine($"Leap year: {cal.IsLeapYear(cal.GetYear(dat))}\n");
Console.WriteLine("Using the Gregorian calendar:");
Console.WriteLine($"Date: {dat:d}");
Console.WriteLine($"Year: {dat.Year}");
Console.WriteLine($"Leap year: {DateTime.IsLeapYear(dat.Year)}");
// The example displays the following output:
// Using the Thai Buddhist Era calendar
// Date : 28/5/2559
// Year: 2559
// Leap year : True
//
// Using the Gregorian calendar
// Date : 28/05/2016
// Year: 2016
// Leap year : True
let thTH = System.Globalization.CultureInfo "th-TH"
let cal = thTH.DateTimeFormat.Calendar
let dat = DateTime(2559, 5, 28, cal)
printfn "Using the Thai Buddhist Era calendar:"
printfn $"""Date: {dat.ToString("d", thTH)}"""
printfn $"Year: {cal.GetYear dat}"
printfn $"Leap year: {cal.IsLeapYear(cal.GetYear dat)}\n"
printfn "Using the Gregorian calendar:"
printfn $"Date: {dat:d}"
printfn $"Year: {dat.Year}"
printfn $"Leap year: {DateTime.IsLeapYear dat.Year}"
// The example displays the following output:
// Using the Thai Buddhist Era calendar
// Date : 28/5/2559
// Year: 2559
// Leap year : True
//
// Using the Gregorian calendar
// Date : 28/05/2016
// Year: 2016
// Leap year : True
Dim thTH As New CultureInfo("th-TH")
Dim cal As Calendar = thTH.DateTimeFormat.Calendar
Dim dat As New DateTime(2559, 5, 28, cal)
Console.WriteLine("Using the Thai Buddhist Era calendar:")
Console.WriteLine($"Date: {dat.ToString("d", thTH)}")
Console.WriteLine($"Year: {cal.GetYear(dat)}")
Console.WriteLine($"Leap year: {cal.IsLeapYear(cal.GetYear(dat))}")
Console.WriteLine()
Console.WriteLine("Using the Gregorian calendar:")
Console.WriteLine($"Date: {dat:d}")
Console.WriteLine($"Year: {dat.Year}")
Console.WriteLine($"Leap year: {DateTime.IsLeapYear(dat.Year)}")
' The example displays the following output:
' Using the Thai Buddhist Era calendar
' Date : 28/5/2559
' Year: 2559
' Leap year : True
'
' Using the Gregorian calendar
' Date : 28/05/2016
' Year: 2016
' Leap year : True
構造体 DateTime には、 DayOfWeek グレゴリオ暦の曜日を返す プロパティが含まれています。 年の週番号を取得できるメンバーは含まれません。 年の週を取得するには、個々のカレンダーの Calendar.GetWeekOfYear メソッドを呼び出します。 具体的な例を次に示します。
var thTH = new System.Globalization.CultureInfo("th-TH");
var thCalendar = thTH.DateTimeFormat.Calendar;
var dat = new DateTime(1395, 8, 18, thCalendar);
Console.WriteLine("Using the Thai Buddhist Era calendar:");
Console.WriteLine($"Date: {dat.ToString("d", thTH)}");
Console.WriteLine($"Day of Week: {thCalendar.GetDayOfWeek(dat)}");
Console.WriteLine($"Week of year: {thCalendar.GetWeekOfYear(dat, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Sunday)}\n");
var greg = new System.Globalization.GregorianCalendar();
Console.WriteLine("Using the Gregorian calendar:");
Console.WriteLine($"Date: {dat:d}");
Console.WriteLine($"Day of Week: {dat.DayOfWeek}");
Console.WriteLine($"Week of year: {greg.GetWeekOfYear(dat, System.Globalization.CalendarWeekRule.FirstDay,DayOfWeek.Sunday)}");
// The example displays the following output:
// Using the Thai Buddhist Era calendar
// Date : 18/8/1395
// Day of Week: Sunday
// Week of year: 34
//
// Using the Gregorian calendar
// Date : 18/08/0852
// Day of Week: Sunday
// Week of year: 34
let thTH = System.Globalization.CultureInfo "th-TH"
let thCalendar = thTH.DateTimeFormat.Calendar
let dat = DateTime(1395, 8, 18, thCalendar)
printfn "Using the Thai Buddhist Era calendar:"
printfn $"""Date: {dat.ToString("d", thTH)}"""
printfn $"Day of Week: {thCalendar.GetDayOfWeek dat}"
printfn $"Week of year: {thCalendar.GetWeekOfYear(dat, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Sunday)}\n"
let greg = System.Globalization.GregorianCalendar()
printfn "Using the Gregorian calendar:"
printfn $"Date: {dat:d}"
printfn $"Day of Week: {dat.DayOfWeek}"
printfn $"Week of year: {greg.GetWeekOfYear(dat, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Sunday)}"
// The example displays the following output:
// Using the Thai Buddhist Era calendar
// Date : 18/8/1395
// Day of Week: Sunday
// Week of year: 34
//
// Using the Gregorian calendar
// Date : 18/08/0852
// Day of Week: Sunday
// Week of year: 34
Dim thTH As New CultureInfo("th-TH")
Dim thCalendar As Calendar = thTH.DateTimeFormat.Calendar
Dim dat As New DateTime(1395, 8, 18, thCalendar)
Console.WriteLine("Using the Thai Buddhist Era calendar:")
Console.WriteLine($"Date: {dat.ToString("d", thTH)}")
Console.WriteLine($"Day of Week: {thCalendar.GetDayOfWeek(dat)}")
Console.WriteLine($"Week of year: {thCalendar.GetWeekOfYear(dat, CalendarWeekRule.FirstDay, DayOfWeek.Sunday)}")
Console.WriteLine()
Dim greg As Calendar = New GregorianCalendar()
Console.WriteLine("Using the Gregorian calendar:")
Console.WriteLine($"Date: {dat:d}")
Console.WriteLine($"Day of Week: {dat.DayOfWeek}")
Console.WriteLine($"Week of year: {greg.GetWeekOfYear(dat, CalendarWeekRule.FirstDay, DayOfWeek.Sunday)}")
' The example displays the following output:
' Using the Thai Buddhist Era calendar
' Date : 18/8/1395
' Day of Week: Sunday
' Week of year: 34
'
' Using the Gregorian calendar
' Date : 18/08/0852
' Day of Week: Sunday
' Week of year: 34
日付と予定表の詳細については、「 予定表の操作」を参照してください。
DateTime 値を保持する
次の方法で値を保持 DateTime できます。
- それらを文字列に変換 し、文字列を保持します。
- それらを 64 ビット整数値 (プロパティの Ticks 値) に変換し、整数を保持します。
- DateTime 値をシリアル化します。
選択した手法に関係なく、値を DateTime 復元するルーチンがデータを失ったり、例外をスローしたりしないようにする必要があります。 DateTime 値はラウンドトリップする必要があります。 つまり、元の値と復元された値は同じである必要があります。 また、元 DateTime の値が 1 回の時間を表す場合は、復元されたのと同じ時間の瞬間を識別する必要があります。
値を文字列として保持する
文字列として保持されている値を正常に復元 DateTime するには、次の規則に従います。
文字列を永続化したときと同じように、文字列を復元する場合は、カルチャ固有の書式設定について同じ前提を持つ必要があります。 現在のカルチャが保存されたシステムのカルチャと異なるシステムで文字列を復元できるようにするには、 オーバーロードを ToString 呼び出して、不変カルチャの規則を使用して文字列を保存します。 または オーバーロードをParse(String, IFormatProvider, DateTimeStyles)TryParse(String, IFormatProvider, DateTimeStyles, DateTime)呼び出して、不変カルチャの規則を使用して文字列を復元します。 現在のカルチャの規則をToString()Parse(String)使用する 、、または TryParse(String, DateTime) オーバーロードは使用しないでください。
日付が 1 つの時刻の瞬間を表す場合は、別のタイム ゾーンであっても、復元された時刻と同じ瞬間を表すようにします。 値を DateTime 保存する前に協定世界時 (UTC) に変換するか、 を使用します DateTimeOffset。
文字列として値を永続化するときに発生する DateTime 最も一般的なエラーは、既定または現在のカルチャの書式設定規則に依存することです。 文字列を保存および復元するときに、現在のカルチャが異なる場合に問題が発生します。 次の例は、これらの問題を示しています。 現在のカルチャの書式設定規則を使用して 5 つの日付を保存します。この場合は英語 (米国)。 別のカルチャの書式設定規則を使用して日付を復元します。この場合は英語 (英国) です。 2 つのカルチャの書式設定規則が異なるため、2 つの日付を復元することはできません。残りの 3 つの日付は正しく解釈されません。 また、元の日付と時刻の値が 1 つの瞬間を表している場合、タイム ゾーン情報が失われるため、復元された時刻は正しくありません。
public static void PersistAsLocalStrings()
{
SaveLocalDatesAsString();
RestoreLocalDatesFromString();
}
private static void SaveLocalDatesAsString()
{
DateTime[] dates = { new DateTime(2014, 6, 14, 6, 32, 0),
new DateTime(2014, 7, 10, 23, 49, 0),
new DateTime(2015, 1, 10, 1, 16, 0),
new DateTime(2014, 12, 20, 21, 45, 0),
new DateTime(2014, 6, 2, 15, 14, 0) };
string? output = null;
Console.WriteLine($"Current Time Zone: {TimeZoneInfo.Local.DisplayName}");
Console.WriteLine($"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:");
for (int ctr = 0; ctr < dates.Length; ctr++)
{
Console.WriteLine(dates[ctr].ToString("f"));
output += dates[ctr].ToString() + (ctr != dates.Length - 1 ? "|" : "");
}
var sw = new StreamWriter(filenameTxt);
sw.Write(output);
sw.Close();
Console.WriteLine("Saved dates...");
}
private static void RestoreLocalDatesFromString()
{
TimeZoneInfo.ClearCachedData();
Console.WriteLine($"Current Time Zone: {TimeZoneInfo.Local.DisplayName}");
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");
StreamReader sr = new StreamReader(filenameTxt);
string[] inputValues = sr.ReadToEnd().Split(new char[] { '|' },
StringSplitOptions.RemoveEmptyEntries);
sr.Close();
Console.WriteLine("The dates on an {0} system:",
Thread.CurrentThread.CurrentCulture.Name);
foreach (var inputValue in inputValues)
{
DateTime dateValue;
if (DateTime.TryParse(inputValue, out dateValue))
{
Console.WriteLine($"'{inputValue}' --> {dateValue:f}");
}
else
{
Console.WriteLine($"Cannot parse '{inputValue}'");
}
}
Console.WriteLine("Restored dates...");
}
// When saved on an en-US system, the example displays the following output:
// Current Time Zone: (UTC-08:00) Pacific Time (US & Canada)
// The dates on an en-US system:
// Saturday, June 14, 2014 6:32 AM
// Thursday, July 10, 2014 11:49 PM
// Saturday, January 10, 2015 1:16 AM
// Saturday, December 20, 2014 9:45 PM
// Monday, June 02, 2014 3:14 PM
// Saved dates...
//
// When restored on an en-GB system, the example displays the following output:
// Current Time Zone: (UTC) Dublin, Edinburgh, Lisbon, London
// The dates on an en-GB system:
// Cannot parse //6/14/2014 6:32:00 AM//
// //7/10/2014 11:49:00 PM// --> 07 October 2014 23:49
// //1/10/2015 1:16:00 AM// --> 01 October 2015 01:16
// Cannot parse //12/20/2014 9:45:00 PM//
// //6/2/2014 3:14:00 PM// --> 06 February 2014 15:14
// Restored dates...
let saveLocalDatesAsString () =
let dates =
[ DateTime(2014, 6, 14, 6, 32, 0)
DateTime(2014, 7, 10, 23, 49, 0)
DateTime(2015, 1, 10, 1, 16, 0)
DateTime(2014, 12, 20, 21, 45, 0)
DateTime(2014, 6, 2, 15, 14, 0) ]
printfn $"Current Time Zone: {TimeZoneInfo.Local.DisplayName}"
printfn $"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:"
let output =
[ for date in dates do
printfn $"{date}"
string date ]
|> String.concat "|"
use sw = new StreamWriter(filenameTxt)
sw.Write output
printfn "Saved dates..."
let restoreLocalDatesFromString () =
TimeZoneInfo.ClearCachedData()
printfn $"Current Time Zone: {TimeZoneInfo.Local.DisplayName}"
Thread.CurrentThread.CurrentCulture <- CultureInfo.CreateSpecificCulture "en-GB"
use sr = new StreamReader(filenameTxt)
let inputValues =
sr.ReadToEnd().Split('|', StringSplitOptions.RemoveEmptyEntries)
printfn $"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:"
for inputValue in inputValues do
match DateTime.TryParse inputValue with
| true, dateValue ->
printfn $"'{inputValue}' --> {dateValue:f}"
| _ ->
printfn $"Cannot parse '{inputValue}'"
printfn "Restored dates..."
let persistAsLocalStrings () =
saveLocalDatesAsString ()
restoreLocalDatesFromString ()
// When saved on an en-US system, the example displays the following output:
// Current Time Zone: (UTC-08:00) Pacific Time (US & Canada)
// The dates on an en-US system:
// Saturday, June 14, 2014 6:32 AM
// Thursday, July 10, 2014 11:49 PM
// Saturday, January 10, 2015 1:16 AM
// Saturday, December 20, 2014 9:45 PM
// Monday, June 02, 2014 3:14 PM
// Saved dates...
//
// When restored on an en-GB system, the example displays the following output:
// Current Time Zone: (UTC) Dublin, Edinburgh, Lisbon, London
// The dates on an en-GB system:
// Cannot parse //6/14/2014 6:32:00 AM//
// //7/10/2014 11:49:00 PM// --> 07 October 2014 23:49
// //1/10/2015 1:16:00 AM// --> 01 October 2015 01:16
// Cannot parse //12/20/2014 9:45:00 PM//
// //6/2/2014 3:14:00 PM// --> 06 February 2014 15:14
// Restored dates...
値のラウンドトリップ DateTime を正常に行うには、次の手順に従います。
- 値が一瞬の時間を表す場合は、 メソッドを呼び出 ToUniversalTime して、ローカル時刻から UTC に変換します。
- または String.Format(IFormatProvider, String, Object[]) オーバーロードを呼び出して、日付を文字列形式にToString(String, IFormatProvider)変換します。 引数として を指定して、インバリアント カルチャの書式設定規則をCultureInfo.InvariantCulture
provider
使用します。 "O" または "R" 標準書式指定文字列を使用して、値をラウンドトリップすることを指定します。
データを失わずに永続化された値を DateTime 復元するには、次の手順に従います。
- または TryParseExact オーバーロードを呼び出してデータをParseExact解析します。 引数として を
provider
指定CultureInfo.InvariantCultureし、変換時に引数に使用したのと同じ標準書式指定文字列をformat
使用します。 引数に DateTimeStyles.RoundtripKind 値をstyles
含めます。 - 値が DateTime 1 つの時点を表す場合は、 メソッドを ToLocalTime 呼び出して、解析された日付を UTC から現地時刻に変換します。
次の例では、インバリアント カルチャと "O" 標準書式指定文字列を使用して、ソース システムとターゲット システムのシステム、カルチャ、タイム ゾーンに関係なく、保存および復元された値が同じ瞬間を表すように DateTime します。
public static void PersistAsInvariantStrings()
{
SaveDatesAsInvariantStrings();
RestoreDatesAsInvariantStrings();
}
private static void SaveDatesAsInvariantStrings()
{
DateTime[] dates = { new DateTime(2014, 6, 14, 6, 32, 0),
new DateTime(2014, 7, 10, 23, 49, 0),
new DateTime(2015, 1, 10, 1, 16, 0),
new DateTime(2014, 12, 20, 21, 45, 0),
new DateTime(2014, 6, 2, 15, 14, 0) };
string? output = null;
Console.WriteLine($"Current Time Zone: {TimeZoneInfo.Local.DisplayName}");
Console.WriteLine($"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:");
for (int ctr = 0; ctr < dates.Length; ctr++)
{
Console.WriteLine(dates[ctr].ToString("f"));
output += dates[ctr].ToUniversalTime().ToString("O", CultureInfo.InvariantCulture)
+ (ctr != dates.Length - 1 ? "|" : "");
}
var sw = new StreamWriter(filenameTxt);
sw.Write(output);
sw.Close();
Console.WriteLine("Saved dates...");
}
private static void RestoreDatesAsInvariantStrings()
{
TimeZoneInfo.ClearCachedData();
Console.WriteLine("Current Time Zone: {0}",
TimeZoneInfo.Local.DisplayName);
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");
StreamReader sr = new StreamReader(filenameTxt);
string[] inputValues = sr.ReadToEnd().Split(new char[] { '|' },
StringSplitOptions.RemoveEmptyEntries);
sr.Close();
Console.WriteLine("The dates on an {0} system:",
Thread.CurrentThread.CurrentCulture.Name);
foreach (var inputValue in inputValues)
{
DateTime dateValue;
if (DateTime.TryParseExact(inputValue, "O", CultureInfo.InvariantCulture,
DateTimeStyles.RoundtripKind, out dateValue))
{
Console.WriteLine($"'{inputValue}' --> {dateValue.ToLocalTime():f}");
}
else
{
Console.WriteLine("Cannot parse '{0}'", inputValue);
}
}
Console.WriteLine("Restored dates...");
}
// When saved on an en-US system, the example displays the following output:
// Current Time Zone: (UTC-08:00) Pacific Time (US & Canada)
// The dates on an en-US system:
// Saturday, June 14, 2014 6:32 AM
// Thursday, July 10, 2014 11:49 PM
// Saturday, January 10, 2015 1:16 AM
// Saturday, December 20, 2014 9:45 PM
// Monday, June 02, 2014 3:14 PM
// Saved dates...
//
// When restored on an en-GB system, the example displays the following output:
// Current Time Zone: (UTC) Dublin, Edinburgh, Lisbon, London
// The dates on an en-GB system:
// '2014-06-14T13:32:00.0000000Z' --> 14 June 2014 14:32
// '2014-07-11T06:49:00.0000000Z' --> 11 July 2014 07:49
// '2015-01-10T09:16:00.0000000Z' --> 10 January 2015 09:16
// '2014-12-21T05:45:00.0000000Z' --> 21 December 2014 05:45
// '2014-06-02T22:14:00.0000000Z' --> 02 June 2014 23:14
// Restored dates...
let saveDatesAsInvariantStrings () =
let dates =
[ DateTime(2014, 6, 14, 6, 32, 0)
DateTime(2014, 7, 10, 23, 49, 0)
DateTime(2015, 1, 10, 1, 16, 0)
DateTime(2014, 12, 20, 21, 45, 0)
DateTime(2014, 6, 2, 15, 14, 0) ]
printfn $"Current Time Zone: {TimeZoneInfo.Local.DisplayName}"
printfn $"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:"
let output =
[ for date in dates do
printfn $"{date:f}"
date.ToUniversalTime().ToString("O", CultureInfo.InvariantCulture) ]
|> String.concat "|"
use sw = new StreamWriter(filenameTxt)
sw.Write output
printfn "Saved dates..."
let restoreDatesAsInvariantStrings () =
TimeZoneInfo.ClearCachedData()
printfn $"Current Time Zone: {TimeZoneInfo.Local.DisplayName}"
Thread.CurrentThread.CurrentCulture <- CultureInfo.CreateSpecificCulture "en-GB"
use sr = new StreamReader(filenameTxt)
let inputValues =
sr.ReadToEnd().Split('|', StringSplitOptions.RemoveEmptyEntries)
printfn $"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:"
for inputValue in inputValues do
match DateTime.TryParseExact(inputValue, "O", CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind) with
| true, dateValue ->
printfn $"'{inputValue}' --> {dateValue.ToLocalTime():f}"
| _ ->
printfn $"Cannot parse '{inputValue}'"
printfn "Restored dates..."
let persistAsInvariantStrings () =
saveDatesAsInvariantStrings ()
restoreDatesAsInvariantStrings ()
// When saved on an en-US system, the example displays the following output:
// Current Time Zone: (UTC-08:00) Pacific Time (US & Canada)
// The dates on an en-US system:
// Saturday, June 14, 2014 6:32 AM
// Thursday, July 10, 2014 11:49 PM
// Saturday, January 10, 2015 1:16 AM
// Saturday, December 20, 2014 9:45 PM
// Monday, June 02, 2014 3:14 PM
// Saved dates...
//
// When restored on an en-GB system, the example displays the following output:
// Current Time Zone: (UTC) Dublin, Edinburgh, Lisbon, London
// The dates on an en-GB system:
// '2014-06-14T13:32:00.0000000Z' --> 14 June 2014 14:32
// '2014-07-11T06:49:00.0000000Z' --> 11 July 2014 07:49
// '2015-01-10T09:16:00.0000000Z' --> 10 January 2015 09:16
// '2014-12-21T05:45:00.0000000Z' --> 21 December 2014 05:45
// '2014-06-02T22:14:00.0000000Z' --> 02 June 2014 23:14
// Restored dates...
値を整数として保持する
日付と時刻は、ティック数を Int64 表す値として保持できます。 この場合、値が永続化および復元されるシステムのカルチャを DateTime 考慮する必要はありません。
値を DateTime 整数として保持するには:
- 値が DateTime 1 つの瞬間を表す場合は、 メソッドを呼び出 ToUniversalTime して UTC に変換します。
- 値によって表されるティック数を DateTime 、その Ticks プロパティから取得します。
整数として永続化されている値を復元 DateTime するには:
- コンストラクターに値をInt64渡して、新しい DateTime オブジェクトをDateTime(Int64)インスタンス化します。
- 値が DateTime 1 つの時点を表す場合は、 メソッドを呼び出して UTC から現地時刻に ToLocalTime 変換します。
次の例では、値の DateTime 配列を整数として、米国太平洋タイム ゾーンのシステムに保持します。 UTC ゾーン内のシステムに復元します。 整数を含むファイルには、その直後の値のInt64合計数を示す値が含まれていますInt32。
public static void PersistAsIntegers()
{
SaveDatesAsInts();
RestoreDatesAsInts();
}
private static void SaveDatesAsInts()
{
DateTime[] dates = { new DateTime(2014, 6, 14, 6, 32, 0),
new DateTime(2014, 7, 10, 23, 49, 0),
new DateTime(2015, 1, 10, 1, 16, 0),
new DateTime(2014, 12, 20, 21, 45, 0),
new DateTime(2014, 6, 2, 15, 14, 0) };
Console.WriteLine($"Current Time Zone: {TimeZoneInfo.Local.DisplayName}");
Console.WriteLine($"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:");
var ticks = new long[dates.Length];
for (int ctr = 0; ctr < dates.Length; ctr++)
{
Console.WriteLine(dates[ctr].ToString("f"));
ticks[ctr] = dates[ctr].ToUniversalTime().Ticks;
}
var fs = new FileStream(filenameInts, FileMode.Create);
var bw = new BinaryWriter(fs);
bw.Write(ticks.Length);
foreach (var tick in ticks)
bw.Write(tick);
bw.Close();
Console.WriteLine("Saved dates...");
}
private static void RestoreDatesAsInts()
{
TimeZoneInfo.ClearCachedData();
Console.WriteLine($"Current Time Zone: {TimeZoneInfo.Local.DisplayName}");
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");
FileStream fs = new FileStream(filenameInts, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
int items;
DateTime[] dates;
try
{
items = br.ReadInt32();
dates = new DateTime[items];
for (int ctr = 0; ctr < items; ctr++)
{
long ticks = br.ReadInt64();
dates[ctr] = new DateTime(ticks).ToLocalTime();
}
}
catch (EndOfStreamException)
{
Console.WriteLine("File corruption detected. Unable to restore data...");
return;
}
catch (IOException)
{
Console.WriteLine("Unspecified I/O error. Unable to restore data...");
return;
}
// Thrown during array initialization.
catch (OutOfMemoryException)
{
Console.WriteLine("File corruption detected. Unable to restore data...");
return;
}
finally
{
br.Close();
}
Console.WriteLine($"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:");
foreach (var value in dates)
Console.WriteLine(value.ToString("f"));
Console.WriteLine("Restored dates...");
}
// When saved on an en-US system, the example displays the following output:
// Current Time Zone: (UTC-08:00) Pacific Time (US & Canada)
// The dates on an en-US system:
// Saturday, June 14, 2014 6:32 AM
// Thursday, July 10, 2014 11:49 PM
// Saturday, January 10, 2015 1:16 AM
// Saturday, December 20, 2014 9:45 PM
// Monday, June 02, 2014 3:14 PM
// Saved dates...
//
// When restored on an en-GB system, the example displays the following output:
// Current Time Zone: (UTC) Dublin, Edinburgh, Lisbon, London
// The dates on an en-GB system:
// 14 June 2014 14:32
// 11 July 2014 07:49
// 10 January 2015 09:16
// 21 December 2014 05:45
// 02 June 2014 23:14
// Restored dates...
let saveDatesAsInts () =
let dates =
[ DateTime(2014, 6, 14, 6, 32, 0)
DateTime(2014, 7, 10, 23, 49, 0)
DateTime(2015, 1, 10, 1, 16, 0)
DateTime(2014, 12, 20, 21, 45, 0)
DateTime(2014, 6, 2, 15, 14, 0) ]
printfn $"Current Time Zone: {TimeZoneInfo.Local.DisplayName}"
printfn $"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:"
let ticks =
[| for date in dates do
printfn $"{date:f}"
date.ToUniversalTime().Ticks |]
use fs = new FileStream(filenameInts, FileMode.Create)
use bw = new BinaryWriter(fs)
bw.Write ticks.Length
for tick in ticks do
bw.Write tick
printfn "Saved dates..."
let restoreDatesAsInts () =
TimeZoneInfo.ClearCachedData()
printfn $"Current Time Zone: {TimeZoneInfo.Local.DisplayName}"
Thread.CurrentThread.CurrentCulture <- CultureInfo.CreateSpecificCulture "en-GB"
use fs = new FileStream(filenameInts, FileMode.Open)
use br = new BinaryReader(fs)
try
let items = br.ReadInt32()
let dates =
[| for _ in 0..items do
let ticks = br.ReadInt64()
DateTime(ticks).ToLocalTime() |]
printfn $"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:"
for value in dates do
printfn $"{value:f}"
with
| :? EndOfStreamException ->
printfn "File corruption detected. Unable to restore data..."
| :? IOException ->
printfn "Unspecified I/O error. Unable to restore data..."
// Thrown during array initialization.
| :? OutOfMemoryException ->
printfn"File corruption detected. Unable to restore data..."
printfn "Restored dates..."
let persistAsIntegers () =
saveDatesAsInts ()
restoreDatesAsInts ()
// When saved on an en-US system, the example displays the following output:
// Current Time Zone: (UTC-08:00) Pacific Time (US & Canada)
// The dates on an en-US system:
// Saturday, June 14, 2014 6:32 AM
// Thursday, July 10, 2014 11:49 PM
// Saturday, January 10, 2015 1:16 AM
// Saturday, December 20, 2014 9:45 PM
// Monday, June 02, 2014 3:14 PM
// Saved dates...
//
// When restored on an en-GB system, the example displays the following output:
// Current Time Zone: (UTC) Dublin, Edinburgh, Lisbon, London
// The dates on an en-GB system:
// 14 June 2014 14:32
// 11 July 2014 07:49
// 10 January 2015 09:16
// 21 December 2014 05:45
// 02 June 2014 23:14
// Restored dates...
DateTime 値をシリアル化する
ストリームまたはファイルへのシリアル化を通じて値を保持 DateTime し、逆シリアル化を通じて復元できます。 DateTime データは、指定されたオブジェクト形式でシリアル化されます。 オブジェクトは、逆シリアル化されるときに復元されます。 または などのJsonSerializerXmlSerializerフォーマッタまたはシリアライザーは、シリアル化と逆シリアル化のプロセスを処理します。 シリアル化と.NET でサポートされるシリアル化の種類の詳細については、「 シリアル化」を参照してください。
次の例では、 クラスを XmlSerializer 使用して値をシリアル化および逆シリアル化 DateTime します。 値は、21 世紀のすべての閏年の日を表します。 現在のカルチャが英語 (英国) のシステムで例が実行されている場合、出力は結果を表します。 オブジェクト自体を DateTime 逆シリアル化したため、コードは日付と時刻の形式のカルチャの違いを処理する必要はありません。
public static void PersistAsXML()
{
// Serialize the data.
var leapYears = new List<DateTime>();
for (int year = 2000; year <= 2100; year += 4)
{
if (DateTime.IsLeapYear(year))
leapYears.Add(new DateTime(year, 2, 29));
}
DateTime[] dateArray = leapYears.ToArray();
var serializer = new XmlSerializer(dateArray.GetType());
TextWriter sw = new StreamWriter(filenameXml);
try
{
serializer.Serialize(sw, dateArray);
}
catch (InvalidOperationException e)
{
Console.WriteLine(e.InnerException?.Message);
}
finally
{
if (sw != null) sw.Close();
}
// Deserialize the data.
DateTime[]? deserializedDates;
using (var fs = new FileStream(filenameXml, FileMode.Open))
{
deserializedDates = (DateTime[]?)serializer.Deserialize(fs);
}
// Display the dates.
Console.WriteLine($"Leap year days from 2000-2100 on an {Thread.CurrentThread.CurrentCulture.Name} system:");
int nItems = 0;
if (deserializedDates is not null)
{
foreach (var dat in deserializedDates)
{
Console.Write($" {dat:d} ");
nItems++;
if (nItems % 5 == 0)
Console.WriteLine();
}
}
}
// The example displays the following output:
// Leap year days from 2000-2100 on an en-GB system:
// 29/02/2000 29/02/2004 29/02/2008 29/02/2012 29/02/2016
// 29/02/2020 29/02/2024 29/02/2028 29/02/2032 29/02/2036
// 29/02/2040 29/02/2044 29/02/2048 29/02/2052 29/02/2056
// 29/02/2060 29/02/2064 29/02/2068 29/02/2072 29/02/2076
// 29/02/2080 29/02/2084 29/02/2088 29/02/2092 29/02/2096
let persistAsXML () =
// Serialize the data.
let leapYears =
[| for year in 2000..4..2100 do
if DateTime.IsLeapYear year then
DateTime(year, 2, 29) |]
let serializer = XmlSerializer(leapYears.GetType())
use sw = new StreamWriter(filenameXml)
try
serializer.Serialize(sw, leapYears)
with :? InvalidOperationException as e ->
printfn $"{e.InnerException.Message}"
// Deserialize the data.
use fs = new FileStream(filenameXml, FileMode.Open)
let deserializedDates = serializer.Deserialize fs :?> DateTime []
// Display the dates.
printfn $"Leap year days from 2000-2100 on an {Thread.CurrentThread.CurrentCulture.Name} system:"
let mutable nItems = 0
for dat in deserializedDates do
printf $" {dat:d} "
nItems <- nItems + 1
if nItems % 5 = 0 then
printfn ""
// The example displays the following output:
// Leap year days from 2000-2100 on an en-GB system:
// 29/02/2000 29/02/2004 29/02/2008 29/02/2012 29/02/2016
// 29/02/2020 29/02/2024 29/02/2028 29/02/2032 29/02/2036
// 29/02/2040 29/02/2044 29/02/2048 29/02/2052 29/02/2056
// 29/02/2060 29/02/2064 29/02/2068 29/02/2072 29/02/2076
// 29/02/2080 29/02/2084 29/02/2088 29/02/2092 29/02/2096
前の例には時間情報は含まれていません。 値が時刻を DateTime 表し、現地時刻として表される場合は、 メソッドを呼び出 ToUniversalTime してシリアル化する前に、ローカル時刻から UTC に変換します。 逆シリアル化した後、 メソッドを呼び出して UTC から現地時刻に変換します ToLocalTime 。
DateTime と TimeSpan
と TimeSpan の値の型はDateTime、 がDateTime一瞬の時間を表すのに対し、 TimeSpan は時間間隔を表す点で異なります。 の 1 つのインスタンスを別の DateTime インスタンスから減算して、 TimeSpan それらの間の時間間隔を表す オブジェクトを取得できます。 または、現在DateTimeの に正TimeSpanの値を追加して、将来の日付をDateTime表す値を取得することもできます。
オブジェクトの DateTime 時間間隔を加算または減算できます。 時間間隔は負または正の値にすることができ、ティック、秒、オブジェクト TimeSpan などの単位で表すことができます。
許容範囲内の等価性を比較する
値の DateTime 等値比較は正確です。 つまり、2 つの値は、等しいと見なされるためには、同じティック数として表す必要があります。 多くの場合、その精度は不要であるか、多くのアプリケーションでは正しくありません。 多くの場合、オブジェクトがほぼ等しいかどうかをDateTimeテストする必要があります。
次の例では、ほぼ同等 DateTime の値を比較する方法を示します。 等しいと宣言するときに、差の小さなマージンを受け入れます。
public static bool RoughlyEquals(DateTime time, DateTime timeWithWindow, int windowInSeconds, int frequencyInSeconds)
{
long delta = (long)((TimeSpan)(timeWithWindow - time)).TotalSeconds % frequencyInSeconds;
delta = delta > windowInSeconds ? frequencyInSeconds - delta : delta;
return Math.Abs(delta) < windowInSeconds;
}
public static void TestRoughlyEquals()
{
int window = 10;
int freq = 60 * 60 * 2; // 2 hours;
DateTime d1 = DateTime.Now;
DateTime d2 = d1.AddSeconds(2 * window);
DateTime d3 = d1.AddSeconds(-2 * window);
DateTime d4 = d1.AddSeconds(window / 2);
DateTime d5 = d1.AddSeconds(-window / 2);
DateTime d6 = (d1.AddHours(2)).AddSeconds(2 * window);
DateTime d7 = (d1.AddHours(2)).AddSeconds(-2 * window);
DateTime d8 = (d1.AddHours(2)).AddSeconds(window / 2);
DateTime d9 = (d1.AddHours(2)).AddSeconds(-window / 2);
Console.WriteLine($"d1 ({d1}) ~= d1 ({d1}): {RoughlyEquals(d1, d1, window, freq)}");
Console.WriteLine($"d1 ({d1}) ~= d2 ({d2}): {RoughlyEquals(d1, d2, window, freq)}");
Console.WriteLine($"d1 ({d1}) ~= d3 ({d3}): {RoughlyEquals(d1, d3, window, freq)}");
Console.WriteLine($"d1 ({d1}) ~= d4 ({d4}): {RoughlyEquals(d1, d4, window, freq)}");
Console.WriteLine($"d1 ({d1}) ~= d5 ({d5}): {RoughlyEquals(d1, d5, window, freq)}");
Console.WriteLine($"d1 ({d1}) ~= d6 ({d6}): {RoughlyEquals(d1, d6, window, freq)}");
Console.WriteLine($"d1 ({d1}) ~= d7 ({d7}): {RoughlyEquals(d1, d7, window, freq)}");
Console.WriteLine($"d1 ({d1}) ~= d8 ({d8}): {RoughlyEquals(d1, d8, window, freq)}");
Console.WriteLine($"d1 ({d1}) ~= d9 ({d9}): {RoughlyEquals(d1, d9, window, freq)}");
}
// The example displays output similar to the following:
// d1 (1/28/2010 9:01:26 PM) ~= d1 (1/28/2010 9:01:26 PM): True
// d1 (1/28/2010 9:01:26 PM) ~= d2 (1/28/2010 9:01:46 PM): False
// d1 (1/28/2010 9:01:26 PM) ~= d3 (1/28/2010 9:01:06 PM): False
// d1 (1/28/2010 9:01:26 PM) ~= d4 (1/28/2010 9:01:31 PM): True
// d1 (1/28/2010 9:01:26 PM) ~= d5 (1/28/2010 9:01:21 PM): True
// d1 (1/28/2010 9:01:26 PM) ~= d6 (1/28/2010 11:01:46 PM): False
// d1 (1/28/2010 9:01:26 PM) ~= d7 (1/28/2010 11:01:06 PM): False
// d1 (1/28/2010 9:01:26 PM) ~= d8 (1/28/2010 11:01:31 PM): True
// d1 (1/28/2010 9:01:26 PM) ~= d9 (1/28/2010 11:01:21 PM): True
let roughlyEquals (time: DateTime) (timeWithWindow: DateTime) windowInSeconds frequencyInSeconds =
let delta =
int64 (timeWithWindow - time).TotalSeconds % frequencyInSeconds
let delta = if delta > windowInSeconds then frequencyInSeconds - delta else delta
abs delta < windowInSeconds
let testRoughlyEquals () =
let window = 10
let window' = 10.
let freq = 60 * 60 * 2 // 2 hours
let d1 = DateTime.Now
let d2 = d1.AddSeconds(2. * window')
let d3 = d1.AddSeconds(-2. * window')
let d4 = d1.AddSeconds(window' / 2.)
let d5 = d1.AddSeconds(-window' / 2.)
let d6 = (d1.AddHours 2).AddSeconds(2. * window')
let d7 = (d1.AddHours 2).AddSeconds(-2. * window')
let d8 = (d1.AddHours 2).AddSeconds(window' / 2.)
let d9 = (d1.AddHours 2).AddSeconds(-window' / 2.)
printfn $"d1 ({d1}) ~= d1 ({d1}): {roughlyEquals d1 d1 window freq}"
printfn $"d1 ({d1}) ~= d2 ({d2}): {roughlyEquals d1 d2 window freq}"
printfn $"d1 ({d1}) ~= d3 ({d3}): {roughlyEquals d1 d3 window freq}"
printfn $"d1 ({d1}) ~= d4 ({d4}): {roughlyEquals d1 d4 window freq}"
printfn $"d1 ({d1}) ~= d5 ({d5}): {roughlyEquals d1 d5 window freq}"
printfn $"d1 ({d1}) ~= d6 ({d6}): {roughlyEquals d1 d6 window freq}"
printfn $"d1 ({d1}) ~= d7 ({d7}): {roughlyEquals d1 d7 window freq}"
printfn $"d1 ({d1}) ~= d8 ({d8}): {roughlyEquals d1 d8 window freq}"
printfn $"d1 ({d1}) ~= d9 ({d9}): {roughlyEquals d1 d9 window freq}"
// The example displays output similar to the following:
// d1 (1/28/2010 9:01:26 PM) ~= d1 (1/28/2010 9:01:26 PM): True
// d1 (1/28/2010 9:01:26 PM) ~= d2 (1/28/2010 9:01:46 PM): False
// d1 (1/28/2010 9:01:26 PM) ~= d3 (1/28/2010 9:01:06 PM): False
// d1 (1/28/2010 9:01:26 PM) ~= d4 (1/28/2010 9:01:31 PM): True
// d1 (1/28/2010 9:01:26 PM) ~= d5 (1/28/2010 9:01:21 PM): True
// d1 (1/28/2010 9:01:26 PM) ~= d6 (1/28/2010 11:01:46 PM): False
// d1 (1/28/2010 9:01:26 PM) ~= d7 (1/28/2010 11:01:06 PM): False
// d1 (1/28/2010 9:01:26 PM) ~= d8 (1/28/2010 11:01:31 PM): True
// d1 (1/28/2010 9:01:26 PM) ~= d9 (1/28/2010 11:01:21 PM): True
Public Shared Function RoughlyEquals(time As DateTime, timeWithWindow As DateTime,
windowInSeconds As Integer,
frequencyInSeconds As Integer) As Boolean
Dim delta As Long = (timeWithWindow.Subtract(time)).TotalSeconds _
Mod frequencyInSeconds
If delta > windowInSeconds Then
delta = frequencyInSeconds - delta
End If
Return Math.Abs(delta) < windowInSeconds
End Function
Public Shared Sub TestRoughlyEquals()
Dim window As Integer = 10
Dim freq As Integer = 60 * 60 * 2 ' 2 hours;
Dim d1 As DateTime = DateTime.Now
Dim d2 As DateTime = d1.AddSeconds(2 * window)
Dim d3 As DateTime = d1.AddSeconds(-2 * window)
Dim d4 As DateTime = d1.AddSeconds(window / 2)
Dim d5 As DateTime = d1.AddSeconds(-window / 2)
Dim d6 As DateTime = d1.AddHours(2).AddSeconds(2 * window)
Dim d7 As DateTime = d1.AddHours(2).AddSeconds(-2 * window)
Dim d8 As DateTime = d1.AddHours(2).AddSeconds(window / 2)
Dim d9 As DateTime = d1.AddHours(2).AddSeconds(-window / 2)
Console.WriteLine($"d1 ({d1}) ~= d1 ({d1}): {RoughlyEquals(d1, d1, window, freq)}")
Console.WriteLine($"d1 ({d1}) ~= d2 ({d2}): {RoughlyEquals(d1, d2, window, freq)}")
Console.WriteLine($"d1 ({d1}) ~= d3 ({d3}): {RoughlyEquals(d1, d3, window, freq)}")
Console.WriteLine($"d1 ({d1}) ~= d4 ({d4}): {RoughlyEquals(d1, d4, window, freq)}")
Console.WriteLine($"d1 ({d1}) ~= d5 ({d5}): {RoughlyEquals(d1, d5, window, freq)}")
Console.WriteLine($"d1 ({d1}) ~= d6 ({d6}): {RoughlyEquals(d1, d6, window, freq)}")
Console.WriteLine($"d1 ({d1}) ~= d7 ({d7}): {RoughlyEquals(d1, d7, window, freq)}")
Console.WriteLine($"d1 ({d1}) ~= d8 ({d8}): {RoughlyEquals(d1, d8, window, freq)}")
Console.WriteLine($"d1 ({d1}) ~= d9 ({d9}): {RoughlyEquals(d1, d9, window, freq)}")
End Sub
' The example displays output similar to the following:
' d1 (1/28/2010 9:01:26 PM) ~= d1 (1/28/2010 9:01:26 PM): True
' d1 (1/28/2010 9:01:26 PM) ~= d2 (1/28/2010 9:01:46 PM): False
' d1 (1/28/2010 9:01:26 PM) ~= d3 (1/28/2010 9:01:06 PM): False
' d1 (1/28/2010 9:01:26 PM) ~= d4 (1/28/2010 9:01:31 PM): True
' d1 (1/28/2010 9:01:26 PM) ~= d5 (1/28/2010 9:01:21 PM): True
' d1 (1/28/2010 9:01:26 PM) ~= d6 (1/28/2010 11:01:46 PM): False
' d1 (1/28/2010 9:01:26 PM) ~= d7 (1/28/2010 11:01:06 PM): False
' d1 (1/28/2010 9:01:26 PM) ~= d8 (1/28/2010 11:01:31 PM): True
' d1 (1/28/2010 9:01:26 PM) ~= d9 (1/28/2010 11:01:21 PM): True
COM 相互運用に関する考慮事項
DateTime COM アプリケーションに転送された後、マネージド アプリケーションに転送される値は、ラウンドトリップと言われます。 ただし、 DateTime 時刻のみを指定する値は、期待どおりにラウンドトリップしません。
午後 3 時などの時刻のみを往復する場合、最終日時は 1899 年 12 月 30 日午後 3:00 (午後 1 月 1 日午前 3 時) です。.NET Frameworkと COM では、時刻のみが指定されている既定の日付が想定されます。 ただし、COM システムは 1899 年 12 月 30 日の基準日を想定し、.NET Frameworkは 0001 年 1 月 1 日の基準日を前提としています。
.NET Frameworkから COM に時刻のみが渡されると、時刻を COM で使用される形式に変換する特別な処理が実行されます。 COM から.NET Frameworkに時刻のみが渡された場合、1899 年 12 月 30 日以前に正当な日付と時刻が破損するため、特別な処理は実行されません。 日付が COM からのラウンド トリップを開始した場合、.NET Frameworkと COM は日付を保持します。
.NET Frameworkと COM の動作は、アプリケーションが時刻のみを指定する をDateTimeラウンドトリップする場合、アプリケーションは最後DateTimeのオブジェクトの誤った日付を変更するか無視することを忘れないようにする必要があることを意味します。
コンストラクター
フィールド
MaxValue |
DateTime の最大有効値を表します。 このフィールドは読み取り専用です。 |
MinValue |
DateTime の最小有効値を表します。 このフィールドは読み取り専用です。 |
UnixEpoch |
この定数の値は、グレゴリオ暦での 1970 年 1 月 1 日 00:00:00.0000000 UTC に相当します。 UnixEpoch によって、UNIX 時刻が 0 に等しい特定の時点が定義されます。 |
プロパティ
Date |
このインスタンスの日付の部分を取得します。 |
Day |
このインスタンスで表される月の日付を取得します。 |
DayOfWeek |
このインスタンスで表される曜日を取得します。 |
DayOfYear |
このインスタンスで表される年間積算日を取得します。 |
Hour |
このインスタンスで表される日付の時間の部分を取得します。 |
Kind |
このインスタンスが表す時刻の種類 (現地時刻、世界協定時刻 (UTC)、または、そのどちらでもない) を示す値を取得します。 |
Microsecond |
マイクロ秒コンポーネント。0 ~ 999 の値として表されます。 |
Millisecond |
このインスタンスで表される日付のミリ秒の部分を取得します。 |
Minute |
このインスタンスで表される日付の分の部分を取得します。 |
Month |
このインスタンスで表される日付の月の部分を取得します。 |
Nanosecond |
nanoseconds コンポーネント。0 ~ 900 (100 ナノ秒単位) の値として表されます。 |
Now |
コンピューター上の現在の日時を現地時刻で表した DateTime オブジェクトを取得します。 |
Second |
このインスタンスで表される日付の秒の部分を取得します。 |
Ticks |
このインスタンスの日付と時刻を表すタイマー刻み数を取得します。 |
TimeOfDay |
このインスタンスの時刻を取得します。 |
Today |
現在の日付を取得します。 |
UtcNow |
コンピューター上の現在の日時を世界協定時刻 (UTC) で表した DateTime オブジェクトを取得します。 |
Year |
このインスタンスで表される日付の年の部分を取得します。 |
メソッド
Add(TimeSpan) | |
AddDays(Double) |
このインスタンスの値に、指定された日数を加算した新しい DateTime を返します。 |
AddHours(Double) |
このインスタンスの値に、指定された時間数を加算した新しい DateTime を返します。 |
AddMicroseconds(Double) |
指定したマイクロ秒数をこのインスタンスの値に加算する新しい DateTime を返します。 |
AddMilliseconds(Double) |
このインスタンスの値に、指定されたミリ秒数を加算した新しい DateTime を返します。 |
AddMinutes(Double) |
このインスタンスの値に、指定された分数を加算した新しい DateTime を返します。 |
AddMonths(Int32) |
このインスタンスの値に、指定された月数を加算した新しい DateTime を返します。 |
AddSeconds(Double) |
このインスタンスの値に、指定された秒数を加算した新しい DateTime を返します。 |
AddTicks(Int64) |
このインスタンスの値に、指定されたタイマー刻みの数を加算した新しい DateTime を返します。 |
AddYears(Int32) |
このインスタンスの値に、指定された年数を加算した新しい DateTime を返します。 |
Compare(DateTime, DateTime) |
DateTime の 2 つのインスタンスを比較し、第 1 のインスタンスが第 2 のインスタンスよりも前か、同じか、それとも後かを示す整数を返します。 |
CompareTo(DateTime) |
このインスタンスの値と指定した DateTime の値を比較し、このインスタンスの値が指定した DateTime の値よりも前か、同じか、または後かを示す整数を返します。 |
CompareTo(Object) |
このインスタンスの値と指定した DateTime の値を含む指定したオブジェクトを比較し、このインスタンスの値が指定した DateTime の値よりも前か、同じか、または後かを示す整数を返します。 |
DaysInMonth(Int32, Int32) |
指定した月および年の日数を返します。 |
Deconstruct(DateOnly, TimeOnly) | |
Deconstruct(Int32, Int32, Int32) | |
Equals(DateTime) |
このインスタンスの値が、指定した DateTime インスタンスの値と等しいかどうかを示す値を返します。 |
Equals(DateTime, DateTime) |
2 つの DateTime インスタンスの日付と時刻の値が同じかどうかを示す値を返します。 |
Equals(Object) |
このインスタンスが指定されたオブジェクトに等しいかどうかを示す値を返します。 |
FromBinary(Int64) |
64 ビットのバイナリ値を逆シリアル化し、元のシリアル化 DateTime オブジェクトを再構築します。 |
FromFileTime(Int64) |
指定された Windows ファイル時刻を同等の現地時刻に変換します。 |
FromFileTimeUtc(Int64) |
指定された Windows ファイル時刻を同等の UTC 時刻に変換します。 |
FromOADate(Double) |
指定した OLE オートメーション日付と等しい DateTime を返します。 |
GetDateTimeFormats() |
このインスタンスの値を、標準の日時形式指定子によってサポートされるすべての文字列形式に変換します。 |
GetDateTimeFormats(Char) |
このインスタンスの値を、指定した標準の日時形式指定子によってサポートされるすべての文字列表記に変換します。 |
GetDateTimeFormats(Char, IFormatProvider) |
このインスタンスの値を、指定した標準日時形式指定子およびカルチャ固有の書式情報によってサポートされる、すべての文字列形式に変換します。 |
GetDateTimeFormats(IFormatProvider) |
このインスタンスの値を、標準日時形式指定子および指定したカルチャ固有の書式情報によってサポートされる、すべての文字列形式に変換します。 |
GetHashCode() |
このインスタンスのハッシュ コードを返します。 |
GetTypeCode() | |
IsDaylightSavingTime() |
DateTime のインスタンスが、現在のタイム ゾーンの夏時間の期間内であるかどうかを示します。 |
IsLeapYear(Int32) |
指定された年が閏年かどうかを示す値を返します。 |
Parse(ReadOnlySpan<Char>, IFormatProvider) |
文字のスパンを値に解析します。 |
Parse(ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles) |
カルチャ固有の書式情報と書式スタイルを使用して、日付と時刻の文字列形式を含むメモリ範囲を等価の DateTime に変換します。 |
Parse(String) |
現在のカルチャの規則を使用して、日付と時刻 DateTime の文字列表現を同等の形式に変換します。 |
Parse(String, IFormatProvider) |
カルチャ固有の書式情報を使用して、日付と時刻の文字列形式を等価の DateTime に変換します。 |
Parse(String, IFormatProvider, DateTimeStyles) |
カルチャ固有の書式情報と書式スタイルを使用して、日付と時刻の文字列形式を等価の DateTime に変換します。 |
ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles) |
指定した書式、カルチャ固有の書式情報、スタイルを使用して、指定した日付と時刻のスパン表現を、それと等価な DateTime に変換します。 文字列形式の書式は、指定した書式と完全に一致する必要があります。それ以外の場合は、例外がスローされます。 |
ParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, DateTimeStyles) |
指定した書式の配列、カルチャ固有の書式情報、スタイルを使用して、指定した日付と時刻のスパン表現を、それと等価な DateTime に変換します。 文字列形式の書式は、指定した書式の少なくとも 1 つと完全に一致する必要があります。それ以外の場合は、例外がスローされます。 |
ParseExact(String, String, IFormatProvider) |
指定した書式とカルチャ固有の書式情報を使用して、指定した日付と時刻の文字列形式を等価の DateTime の値に変換します。 文字列形式の書式は、指定した書式と完全に一致する必要があります。 |
ParseExact(String, String, IFormatProvider, DateTimeStyles) |
指定した書式、カルチャ固有の書式情報、およびスタイルを使用して、指定した日付と時刻の文字列形式を等価の DateTime に変換します。 文字列形式の書式は、指定した書式と完全に一致する必要があります。それ以外の場合は、例外がスローされます。 |
ParseExact(String, String[], IFormatProvider, DateTimeStyles) |
指定した書式の配列、カルチャ固有の書式情報、およびスタイルを使用して、指定した日付と時刻の文字列形式を等価の DateTime に変換します。 文字列形式の書式は、指定した書式の少なくとも 1 つと完全に一致する必要があります。それ以外の場合は、例外がスローされます。 |
SpecifyKind(DateTime, DateTimeKind) |
指定された DateTime と同じタイマー刻みの数を持つ新しい DateTime オブジェクトを、指定された DateTimeKind 値 (現地時刻、世界協定時刻 (UTC)、または、そのいずれでもないことを示す) に基づいて作成します。 |
Subtract(DateTime) |
このインスタンスの値から指定した日時を減算した、新しい TimeSpan を返します。 |
Subtract(TimeSpan) |
このインスタンスの値から指定した期間を減算した、新しい DateTime を返します。 |
ToBinary() |
現在の DateTime オブジェクトを 64 ビットのバイナリ値にシリアル化します。後で、この値を使って、DateTime オブジェクトを再構築できます。 |
ToFileTime() |
現在の DateTime オブジェクトの値を Windows ファイル時刻に変換します。 |
ToFileTimeUtc() |
現在の DateTime オブジェクトの値を Windows ファイル時刻に変換します。 |
ToLocalTime() |
現在の DateTime オブジェクトの値を現地時刻に変換します。 |
ToLongDateString() |
現在の DateTime オブジェクトの値を、それと等価な長い形式の日付の文字列形式に変換します。 |
ToLongTimeString() |
現在の DateTime オブジェクトの値を、それと等価な長い形式の時刻の文字列形式に変換します。 |
ToOADate() |
このインスタンスの値をそれと等価な OLE オートメーション日付に変換します。 |
ToShortDateString() |
現在の DateTime オブジェクトの値を、それと等価な短い形式の日付の文字列形式に変換します。 |
ToShortTimeString() |
現在の DateTime オブジェクトの値を、それと等価な短い形式の時刻の文字列形式に変換します。 |
ToString() |
現在のカルチャの書式指定規則を使用して、現在の DateTime オブジェクトの値をそれと等価な文字列形式に変換します。 |
ToString(IFormatProvider) |
指定したカルチャ固有の書式情報を使用して、現在の DateTime オブジェクトの値をそれと等価な文字列形式に変換します。 |
ToString(String) |
指定した形式および現在のカルチャの書式指定規則を使用して、現在の DateTime オブジェクトの値をそれと等価な文字列形式に変換します。 |
ToString(String, IFormatProvider) |
指定した書式およびカルチャ固有の書式情報を使用して、現在の DateTime オブジェクトの値をそれと等価の文字列形式に変換します。 |
ToUniversalTime() |
現在の DateTime オブジェクトの値を世界協定時刻 (UTC) に変換します。 |
TryFormat(Span<Byte>, Int32, ReadOnlySpan<Char>, IFormatProvider) |
現在のインスタンスの値を UTF-8 として指定されたバイトスパンに書式設定しようとします。 |
TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider) |
現在の DateTime 型のインスタンスの値を、指定した文字スパンに書式設定しようとします。 |
TryParse(ReadOnlySpan<Char>, DateTime) |
指定した日付と時刻の文字スパンを、それと等価な DateTime に変換し、変換が成功したかどうかを示す値を返します。 |
TryParse(ReadOnlySpan<Char>, IFormatProvider, DateTime) |
文字のスパンを値に解析しようとします。 |
TryParse(ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles, DateTime) |
指定したカルチャ固有の書式情報と書式スタイルを使用して、日付と時刻のスパン表現を、それと等価な DateTime に変換し、変換が成功したかどうかを示す値を返します。 |
TryParse(String, DateTime) |
指定した文字列形式の日時を対応する DateTime 表現に変換し、変換に成功したかどうかを示す値を返します。 |
TryParse(String, IFormatProvider, DateTime) |
文字列を値に解析しようとします。 |
TryParse(String, IFormatProvider, DateTimeStyles, DateTime) |
指定したカルチャ固有の書式情報と書式スタイルを使用して、指定した文字列形式の日付と時刻をそれと等価の DateTime に変換し、変換に成功したかどうかを示す値を返します。 |
TryParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles, DateTime) |
指定した書式、カルチャ固有の書式情報、スタイルを使用して、指定した日付と時刻のスパン表現を、それと等価な DateTime に変換します。 文字列形式の書式は、指定した書式と完全に一致する必要があります。 このメソッドは、変換に成功したかどうかを示す値を返します。 |
TryParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, DateTimeStyles, DateTime) |
指定した日付と時刻の文字スパンを、それと等価な DateTime に変換し、変換が成功したかどうかを示す値を返します。 |
TryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTime) |
指定した書式、カルチャ固有の書式情報、およびスタイルを使用して、指定した日付と時刻の文字列形式を等価の DateTime に変換します。 文字列形式の書式は、指定した書式と完全に一致する必要があります。 このメソッドは、変換に成功したかどうかを示す値を返します。 |
TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTime) |
指定した書式の配列、カルチャ固有の書式情報、およびスタイルを使用して、指定した日付と時刻の文字列形式を等価の DateTime に変換します。 文字列形式の書式は、指定した書式の少なくとも 1 つと完全に一致する必要があります。 このメソッドは、変換に成功したかどうかを示す値を返します。 |
演算子
Addition(DateTime, TimeSpan) |
指定した日付と時刻に指定した時間間隔を加算して、新しい日付と時刻を作成します。 |
Equality(DateTime, DateTime) |
DateTime の 2 つの指定したインスタンスが等しいかどうかを判断します。 |
GreaterThan(DateTime, DateTime) | |
GreaterThanOrEqual(DateTime, DateTime) |
指定した 1 つの DateTime が、指定した別の DateTime と同じ日時またはそれより後の日時を表しているかどうかを判断します。 |
Inequality(DateTime, DateTime) |
DateTime の 2 つの指定したインスタンスが等しいかどうかを判断します。 |
LessThan(DateTime, DateTime) | |
LessThanOrEqual(DateTime, DateTime) |
指定した 1 つの DateTime が、指定した別の DateTime と同じ日時またはそれより前の日時を表しているかどうかを判断します。 |
Subtraction(DateTime, DateTime) |
指定した日付と時刻から指定したもう 1 つの日付と時刻を減算して、時間間隔を返します。 |
Subtraction(DateTime, TimeSpan) |
指定した日付と時刻から指定した時間間隔を減算して、新しい日付と時刻を返します。 |
明示的なインターフェイスの実装
適用対象
スレッド セーフ
この型のすべてのメンバーはスレッド セーフです。 インスタンスの状態を変更するように見えるメンバーは、実際には新しい値で初期化された新しいインスタンスを返します。 他の型と同様に、この型のインスタンスを含む共有変数の読み取りと書き込みは、スレッドセーフを保証するためにロックによって保護する必要があります。
こちらもご覧ください
フィードバック
フィードバックの送信と表示