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, 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 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
[<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, 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
する - 文字列を既知の形式で使用
ParseExact
またはTryParseExact
変換する - ISO 8601 文字列形式から日付と時刻への変換
DateTime
解像 度
カルチャとカレンダー
- カルチャ固有のカレンダーを使用して日付と時刻の値を表示する
- カルチャ固有のカレンダーに従って文字列を解析する
- 特定のカルチャの予定表から日付と時刻を初期化する
- 特定のカルチャの予定表を使用して日付と時刻のプロパティにアクセスする
- カルチャ固有のカレンダーを使用して年の週を取得する
永続化
- 日付と時刻の値を文字列としてローカル タイム ゾーンに保持する
- 日付と時刻の値をカルチャと時刻のインバリアント形式で文字列として保持する
- 日付と時刻の値を整数として保持する
- を使用して日付と時刻の値を保持する
XmlSerializer
- を使用して日付と時刻の値を保持する
BinaryFormatter
- タイム ゾーン データを使用して日付と時刻の値を保持する
解説トピックへのクイック リンク。
このセクションには、構造体の多くの一般的な使用方法に関するトピックが DateTime
含まれています。
- オブジェクトの
DateTime
初期化 - DateTime 値とその文字列表現
- 文字列からの DateTime 値の解析
- DateTime 値
- DateTime 操作
- DateTime の解決
- DateTime の値と予定表
- DateTime 値の永続化
- DateTime と TimeSpan
- 許容範囲内の等価性の比較
- COM 相互運用に関する考慮事項
値型は DateTime 、00:00:00 (午前 0 時)、0001 年 1 月 1 日の Anno Domini (Common Era) から 9999 年 12 月 31 日午後 11 時 59 分 59 分までの値を持つ日付と時刻を表します。 です。
時間の値は、ティックと呼ばれる 100 ナノ秒単位で測定されます。 特定の日付は、0001 年 1 月 1 日午前 0 時 00 分からのティック数です。 (C.E.)予定表に表示されます GregorianCalendar 。 この数値は、閏秒で加算されるティックを除外します。 たとえば、ティック値 312413760000000000L は、0100 年 1 月 1 日金曜日の午前 12 時 00 分 00 分の日付を表します。 DateTime値は、常に明示的または既定の予定表のコンテキストで表されます。
注意
分や秒などの他の時間間隔に変換するティック値を使用する場合は、変換を実行するために 、、TimeSpan.TicksPerHour、、TimeSpan.TicksPerMinuteTimeSpan.TicksPerSecondまたはTimeSpan.TicksPerMillisecond定数を使用TimeSpan.TicksPerDayする必要があります。 たとえば、指定したティック数で表される秒数を値のDateTimeコンポーネントにSecond追加するには、式dateValue.Second + nTicks/Timespan.TicksPerSecond
を使用できます。
この記事の一連の例全体のソースは、GitHub のドキュメント リポジトリの Visual Basic、 F#、または C# で確認できます。
注意
特定のタイム ゾーンで日付と時刻の DateTime 値を操作するための構造の代わりに、構造体があります DateTimeOffset 。 構造体は DateTimeOffset 、日付と時刻の情報をプライベート DateTime フィールドに格納し、その日付と時刻がプライベート Int16 フィールドの UTC と異なる分数を格納します。 これにより、 DateTimeOffset 値に特定のタイム ゾーンの時刻を反映させることができますが DateTime 、値は UTC とローカル タイム ゾーンの時刻のみを明確に反映できます。 日付と時刻の値を操作するときに構造体または構造体を使用DateTimeするタイミングについては、「DateTime、DateTimeOffset、TimeSpan、TimeZoneInfo の選択」を参照してください。DateTimeOffset
DateTime オブジェクトの初期化
初期値は、さまざまな方法で新しい DateTime
値に割り当てることができます。
- コンストラクターを呼び出します。値の引数を指定するか、暗黙的なパラメーターなしのコンストラクターを使用します。
DateTime
プロパティまたはメソッドの戻り値に a を割り当てる。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 明示的に割り当てずに値を宣言することもできます。 明示的な初期化なしで値を作成すると、既定値にもなります。 次の例は、C# と Visual Basic の暗黙的なパラメーターなしのコンストラクターと、DateTimeVisual 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、文字列を等価の日付と時刻の値に変換します。 次の例では、and メソッドをParse使用して文字列を解析し、それを値にDateTime変換ParseExactします。 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)
and TryParseExact メソッドはTryParse、文字列が値のDateTime有効な表現であるかどうかを示し、有効な場合は変換を実行します。
Visual Basic の言語固有の構文
次の Visual Basic ステートメントは、新しい DateTime 値を初期化します。
Dim date1 As Date = #5/1/2008 8:30:52AM#
DateTime 値とその文字列表現
内部的には、すべての DateTime 値は、0001 年 1 月 1 日午前 12:00: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
メソッドをTryParseExact使用して、特定のParseExact形式または形式に一致する必要がある文字列をDateTime値に変換します。 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 and ParseExact メソッドは例外をスローします。 and TryParseExact メソッドはTryParse、Boolean変換が成功したか失敗したかを示す値を返します。 パフォーマンスが TryParse 重要なシナリオでは、これらのメソッドを TryParseExact 使用する必要があります。 日付と時刻の文字列の解析操作は、エラー率が高くなる傾向があり、例外処理はコストがかかります。 文字列がユーザーによって入力される場合、または不明なソースから取得される場合は、これらのメソッドを使用します。
日付と時刻の値の解析の詳細については、「 日付と時刻の文字列の解析」を参照してください。
DateTime 値
型の時刻値 DateTime の説明は、多くの場合、協定世界時 (UTC) 標準を使用して表されます。 協定世界時は、グリニッジ標準時 (GMT) の国際的に認められた名前です。 協定世界時は、経度 0 度 (UTC の起点) で測定された時刻です。 夏時間は 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 ミリ秒の遅延が発生します。 次の例は、呼び出しThread.Sleep後にのみプロパティの変更によってDateTime.Now.Milliseconds
返されるミリ秒数を示しています。
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 操作
構造体を使用したDateTime計算 (または Subtract、 などAdd) は、構造体の値を変更しません。 代わりに、計算は、その値が計算の結果である新しい DateTime 構造体を返します。
タイム ゾーン間 (UTC と現地時刻の間、またはあるタイム ゾーンと別のタイム ゾーン間など) 間の変換操作では夏時間が考慮されますが、算術演算と比較操作では考慮されません。
構造体自体では、あるタイム ゾーンから別のタイム ゾーンへの変換のサポートが DateTime 制限されています。 このメソッドを ToLocalTime 使用して UTC を現地時刻に変換することも、メソッドを ToUniversalTime 使用して現地時刻から UTC に変換することもできます。 ただし、このクラスでは、タイム ゾーン変換メソッドの完全なセットを TimeZoneInfo 使用できます。 これらの方法を使用して、世界のいずれかのタイム ゾーンの時刻を他のタイム ゾーンの時刻に変換します。
オブジェクトの DateTime 計算と比較は、オブジェクトが同じタイム ゾーンの時刻を表す場合にのみ意味があります。 オブジェクトを TimeZoneInfo 使用して値のタイム ゾーンを DateTime 表すことができますが、2 つは疎結合されています。 DateTimeオブジェクトには、その日付と時刻の値のタイム ゾーンを表すオブジェクトを返すプロパティがありません。 このプロパティは Kind 、 DateTime
UTC、現地時刻、または指定されていないかどうかを示します。 タイム ゾーン対応アプリケーションでは、オブジェクトが作成されたタイム ゾーンを特定するために、外部メカニズムに依存する DateTime 必要があります。 値と値のタイム ゾーンを表すオブジェクトの DateTime 両方を TimeZoneInfo ラップする構造体を DateTime 使用できます。 計算での UTC の使用と値との DateTime 比較の詳細については、「 日付と時刻を使用した算術演算の実行」を参照してください。
各 DateTime メンバーは、グレゴリオ暦を暗黙的に使用してその操作を実行します。 例外は、カレンダーを暗黙的に指定するメソッドです。 これには、カレンダーを指定するコンストラクター、および派生した IFormatProviderパラメーターを持つメソッドが含まれます。次に例を示 System.Globalization.DateTimeFormatInfoします。
この型の DateTime メンバーによる操作では、閏年や 1 か月の日数などの詳細が考慮されます。
DateTime の値と予定表
.NET Framework クラス ライブラリには、クラスから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 値の永続化
次の 4 つの方法で値を保持 DateTime できます。
- それらを文字列に変換し、文字列を保持します。
- 64 ビット整数値 (プロパティのTicks値) に変換し、整数を保持します。
- DateTime 値をシリアル化します。
- DateTime 値とタイム ゾーン情報をシリアル化します。
値を復元 DateTime するルーチンが、選択した手法に関係なくデータを失ったり、例外をスローしたりしないようにする必要があります。 DateTime 値はラウンドトリップする必要があります。 つまり、元の値と復元された値は同じである必要があります。 また、元 DateTime の値が 1 回の瞬間を表す場合は、復元された同じ時刻を識別する必要があります。
値を文字列として永続化する
文字列として保持されている値を正常に復元 DateTime するには、次の規則に従います。
文字列を永続化したときと同じように、カルチャ固有の書式を復元する場合と同じ前提を設定します。 現在のカルチャが保存されたシステムのカルチャと異なるシステムで文字列を復元できるようにするには、オーバーロードを ToString 呼び出して、不変カルチャの規則を使用して文字列を保存します。 インバリアント カルチャの Parse(String, IFormatProvider, DateTimeStyles) 規則を使用して文字列を復元するには、or TryParse(String, IFormatProvider, DateTimeStyles, DateTime) オーバーロードを呼び出します。 現在のカルチャの規則を ToString()使用する 、 Parse(String)または TryParse(String, DateTime) オーバーロードを使用しないでください。
日付が 1 つの時刻を表す場合は、別のタイム ゾーンであっても、復元された時刻と同じ時刻を表すようにします。 保存する前に、 DateTime 値を協定世界時 (UTC) に変換します。 タイム ゾーン情報と共に値をシリアル化することもできます。 この方法の詳細については、「 DateTime データとタイム ゾーン データのシリアル化」を参照してください。
文字列として値を永続化するときに発生する 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...
Public Sub PersistAsLocalStrings()
SaveDatesAsStrings()
RestoreDatesAsStrings()
End Sub
Private Sub SaveDatesAsStrings()
Dim dates As Date() = {#6/14/2014 6:32AM#, #7/10/2014 11:49PM#,
#1/10/2015 1:16AM#, #12/20/2014 9:45PM#,
#6/2/2014 3:14PM#}
Dim output As String = Nothing
Console.WriteLine($"Current Time Zone: {TimeZoneInfo.Local.DisplayName}")
Console.WriteLine($"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:")
For ctr As Integer = 0 To dates.Length - 1
Console.WriteLine(dates(ctr).ToString("f"))
output += dates(ctr).ToString() + If(ctr <> dates.Length - 1, "|", "")
Next
Dim sw As New StreamWriter(filenameTxt)
sw.Write(output)
sw.Close()
Console.WriteLine("Saved dates...")
End Sub
Private Sub RestoreDatesAsStrings()
TimeZoneInfo.ClearCachedData()
Console.WriteLine($"Current Time Zone: {TimeZoneInfo.Local.DisplayName}")
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB")
Dim sr As New StreamReader(filenameTxt)
Dim inputValues As String() = sr.ReadToEnd().Split({"|"c}, StringSplitOptions.RemoveEmptyEntries)
sr.Close()
Console.WriteLine($"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:")
For Each inputValue In inputValues
Dim dateValue As Date
If DateTime.TryParse(inputValue, dateValue) Then
Console.WriteLine($"'{inputValue}' --> {dateValue:f}")
Else
Console.WriteLine($"Cannot parse '{inputValue}'")
End If
Next
Console.WriteLine("Restored dates...")
End Sub
' 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.RoundtripKindstyles
含めます。 - 値が 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...
Public Sub PersistAsInvariantStrings()
SaveDatesAsInvariantStrings()
RestoreDatesAsInvariantStrings()
End Sub
Private Sub SaveDatesAsInvariantStrings()
Dim dates As Date() = {#6/14/2014 6:32AM#, #7/10/2014 11:49PM#,
#1/10/2015 1:16AM#, #12/20/2014 9:45PM#,
#6/2/2014 3:14PM#}
Dim output As String = Nothing
Console.WriteLine($"Current Time Zone: {TimeZoneInfo.Local.DisplayName}")
Console.WriteLine($"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:")
For ctr As Integer = 0 To dates.Length - 1
Console.WriteLine(dates(ctr).ToString("f"))
output += dates(ctr).ToUniversalTime().ToString("O", CultureInfo.InvariantCulture) +
If(ctr <> dates.Length - 1, "|", "")
Next
Dim sw As New StreamWriter(filenameTxt)
sw.Write(output)
sw.Close()
Console.WriteLine("Saved dates...")
End Sub
Private Sub RestoreDatesAsInvariantStrings()
TimeZoneInfo.ClearCachedData()
Console.WriteLine("Current Time Zone: {0}",
TimeZoneInfo.Local.DisplayName)
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB")
Dim sr As New StreamReader(filenameTxt)
Dim inputValues As String() = sr.ReadToEnd().Split({"|"c}, StringSplitOptions.RemoveEmptyEntries)
sr.Close()
Console.WriteLine($"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:")
For Each inputValue In inputValues
Dim dateValue As Date
If DateTime.TryParseExact(inputValue, "O", CultureInfo.InvariantCulture,
DateTimeStyles.RoundtripKind, dateValue) Then
Console.WriteLine($"'{inputValue}' --> {dateValue.ToLocalTime():f}")
Else
Console.WriteLine($"Cannot parse '{inputValue}'")
End If
Next
Console.WriteLine("Restored dates...")
End Sub
' 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 に変換します。
- そのプロパティから値によって表されるティックの数をDateTimeTicks取得します。
整数として永続化されている値を復元 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...
Public Sub PersistAsIntegers()
SaveDatesAsIntegers()
RestoreDatesAsIntegers()
End Sub
Private Sub SaveDatesAsIntegers()
Dim dates As Date() = {#6/14/2014 6:32AM#, #7/10/2014 11:49PM#,
#1/10/2015 1:16AM#, #12/20/2014 9:45PM#,
#6/2/2014 3:14PM#}
Console.WriteLine($"Current Time Zone: {TimeZoneInfo.Local.DisplayName}")
Console.WriteLine($"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:")
Dim ticks(dates.Length - 1) As Long
For ctr As Integer = 0 To dates.Length - 1
Console.WriteLine(dates(ctr).ToString("f"))
ticks(ctr) = dates(ctr).ToUniversalTime().Ticks
Next
Dim fs As New FileStream(filenameInts, FileMode.Create)
Dim bw As New BinaryWriter(fs)
bw.Write(ticks.Length)
For Each tick In ticks
bw.Write(tick)
Next
bw.Close()
Console.WriteLine("Saved dates...")
End Sub
Private Sub RestoreDatesAsIntegers()
TimeZoneInfo.ClearCachedData()
Console.WriteLine($"Current Time Zone: {TimeZoneInfo.Local.DisplayName}")
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB")
Dim fs As New FileStream(filenameInts, FileMode.Open)
Dim br As New BinaryReader(fs)
Dim items As Integer
Dim dates As DateTime()
Try
items = br.ReadInt32()
ReDim dates(items - 1)
For ctr As Integer = 0 To items - 1
Dim ticks As Long = br.ReadInt64()
dates(ctr) = New DateTime(ticks).ToLocalTime()
Next
Catch e As EndOfStreamException
Console.WriteLine("File corruption detected. Unable to restore data...")
Exit Sub
Catch e As IOException
Console.WriteLine("Unspecified I/O error. Unable to restore data...")
Exit Sub
Catch e As OutOfMemoryException 'Thrown in array initialization.
Console.WriteLine("File corruption detected. Unable to restore data...")
Exit Sub
Finally
br.Close()
End Try
Console.WriteLine($"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:")
For Each value In dates
Console.WriteLine(value.ToString("f"))
Next
Console.WriteLine("Restored dates...")
End Sub
' 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 データは、指定されたオブジェクト形式でシリアル化されます。 オブジェクトは、逆シリアル化されると復元されます。 フォーマッタまたはシリアライザー (シリアル化やBinaryFormatterシリアル化などXmlSerializer) は、シリアル化と逆シリアル化のプロセスを処理します。 シリアル化と、.NET Frameworkでサポートされるシリアル化の種類の詳細については、「シリアル化」を参照してください。
次の例では、クラスを 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;
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
Public Sub PersistAsXml()
' Serialize the data.
Dim leapYears As New List(Of DateTime)()
For year As Integer = 2000 To 2100 Step 4
If Date.IsLeapYear(year) Then
leapYears.Add(New Date(year, 2, 29))
End If
Next
Dim dateArray As DateTime() = leapYears.ToArray()
Dim serializer As New XmlSerializer(dateArray.GetType())
Dim sw As TextWriter = New StreamWriter(filenameXml)
Try
serializer.Serialize(sw, dateArray)
Catch e As InvalidOperationException
Console.WriteLine(e.InnerException.Message)
Finally
If sw IsNot Nothing Then sw.Close()
End Try
' Deserialize the data.
Dim deserializedDates As Date()
Using fs As New FileStream(filenameXml, FileMode.Open)
deserializedDates = CType(serializer.Deserialize(fs), Date())
End Using
' Display the dates.
Console.WriteLine($"Leap year days from 2000-2100 on an {Thread.CurrentThread.CurrentCulture.Name} system:")
Dim nItems As Integer
For Each dat In deserializedDates
Console.Write($" {dat:d} ")
nItems += 1
If nItems Mod 5 = 0 Then Console.WriteLine()
Next
End Sub
' 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 。 次の例では、クラスを BinaryFormatter 使用して、米国太平洋標準タイム ゾーンのシステム上のデータをシリアル化 DateTime し、米国中部標準ゾーンのシステムで逆シリアル化します。
public static void PersistBinary()
{
SaveDatesBinary();
RestoreDatesBinary();
}
private static void SaveDatesBinary()
{
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) };
var fs = new FileStream(filenameBin, FileMode.Create);
var bin = new BinaryFormatter();
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"));
dates[ctr] = dates[ctr].ToUniversalTime();
}
bin.Serialize(fs, dates);
fs.Close();
Console.WriteLine("Saved dates...");
}
private static void RestoreDatesBinary()
{
TimeZoneInfo.ClearCachedData();
Console.WriteLine($"Current Time Zone: {TimeZoneInfo.Local.DisplayName}");
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");
FileStream fs = new FileStream(filenameBin, FileMode.Open);
BinaryFormatter bin = new BinaryFormatter();
var dates = (DateTime[])bin.Deserialize(fs);
fs.Close();
Console.WriteLine($"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:");
foreach (var value in dates)
Console.WriteLine(value.ToLocalTime().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-6:00) Central Time (US & Canada)
// The dates on an en-GB system:
// 14 June 2014 08:32
// 11 July 2014 01:49
// 10 January 2015 03:16
// 20 December 2014 23:45
// 02 June 2014 17:14
// Restored dates...
// BinaryFormatter serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information.
let saveDatesBinary () =
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) |]
|> Array.map (fun date ->
printfn $"{date:f}"
date.ToUniversalTime() )
use fs = new FileStream(filenameBin, FileMode.Create)
let bin = BinaryFormatter()
printfn $"Current Time Zone: {TimeZoneInfo.Local.DisplayName}"
printfn $"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:"
bin.Serialize(fs, dates)
printfn "Saved dates..."
let restoreDatesBinary () =
TimeZoneInfo.ClearCachedData()
printfn $"Current Time Zone: {TimeZoneInfo.Local.DisplayName}"
Thread.CurrentThread.CurrentCulture <- CultureInfo.CreateSpecificCulture "en-GB"
use fs = new FileStream(filenameBin, FileMode.Open)
let bin = BinaryFormatter()
let dates = bin.Deserialize fs :?> DateTime []
printfn $"The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:"
for value in dates do
printfn $"{value.ToLocalTime():f}"
printfn "Restored dates..."
let persistBinary () =
saveDatesBinary ()
restoreDatesBinary ()
// 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-6:00) Central Time (US & Canada)
// The dates on an en-GB system:
// 14 June 2014 08:32
// 11 July 2014 01:49
// 10 January 2015 03:16
// 20 December 2014 23:45
// 02 June 2014 17:14
// Restored dates...
Public Sub PersistBinary()
SaveDatesBinary()
RestoreDatesBinary()
End Sub
Private Sub SaveDatesBinary()
Dim dates As Date() = {#6/14/2014 6:32AM#, #7/10/2014 11:49PM#,
#1/10/2015 1:16AM#, #12/20/2014 9:45PM#,
#6/2/2014 3:14PM#}
Dim fs As New FileStream(filenameBin, FileMode.Create)
Dim bin As New BinaryFormatter()
Console.WriteLine($"Current Time Zone: {TimeZoneInfo.Local.DisplayName}")
Console.WriteLine("The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:")
For ctr As Integer = 0 To dates.Length - 1
Console.WriteLine(dates(ctr).ToString("f"))
dates(ctr) = dates(ctr).ToUniversalTime()
Next
bin.Serialize(fs, dates)
fs.Close()
Console.WriteLine("Saved dates...")
End Sub
Private Sub RestoreDatesBinary()
TimeZoneInfo.ClearCachedData()
Console.WriteLine("Current Time Zone: {TimeZoneInfo.Local.DisplayName}")
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB")
Dim fs As New FileStream(filenameBin, FileMode.Open)
Dim bin As New BinaryFormatter()
Dim dates As DateTime() = DirectCast(bin.Deserialize(fs), Date())
fs.Close()
Console.WriteLine("The dates on an {Thread.CurrentThread.CurrentCulture.Name} system:")
For Each value In dates
Console.WriteLine(value.ToLocalTime().ToString("f"))
Next
Console.WriteLine("Restored dates...")
End Sub
' 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-6:00) Central Time (US & Canada)
' The dates on an en-GB system:
' 14 June 2014 08:32
' 11 July 2014 01:49
' 10 January 2015 03:16
' 20 December 2014 11:45
' 02 June 2014 17:14
' Restored dates...
DateTime データとタイム ゾーン データのシリアル化
前の例はすべて、値が DateTime ローカル時刻として表されることを前提としています。 コードは UTC とローカル時刻の間で値を変換し、ソース システムとターゲット システムで同じ時点を反映しました。 DateTime 値には、ローカルと UTC 以外のタイム ゾーン内の時間の瞬間も反映される場合があります。 DateTime構造体はタイム ゾーン対応ではないため、値とそのタイム ゾーンを表すオブジェクトのDateTime両方をTimeZoneInfoシリアル化する必要があります。 値とそのタイム ゾーンの両方を含むフィールドを DateTime 持つ型を作成します。 次の例では、構造体を DateWithTimeZone
定義します。
using System;
namespace DateTimeExtensions
{
[Serializable]
public struct DateWithTimeZone
{
private TimeZoneInfo tz;
private DateTime dt;
public DateWithTimeZone(DateTime dateValue, TimeZoneInfo timeZone)
{
dt = dateValue;
tz = timeZone ?? TimeZoneInfo.Local;
}
public TimeZoneInfo TimeZone
{
get { return (tz); }
set { tz = value; }
}
public DateTime DateTime
{
get { return (dt); }
set { dt = value; }
}
}
}
namespace DateTimeExtensions
open System
[<Serializable>]
type DateWithTimeZone =
struct
val TimeZone: TimeZoneInfo
val DateTime: DateTime
new (dateValue, timeZone) =
{ DateTime = dateValue;
TimeZone =
if isNull timeZone then TimeZoneInfo.Local
else timeZone }
end
Namespace DateTimeExtensions
<Serializable> Public Structure DateWithTimeZone
Private tz As TimeZoneInfo
Private dt As DateTime
Public Sub New(dateValue As DateTime, timeZone As TimeZoneInfo)
dt = dateValue
tz = If(timeZone, TimeZoneInfo.Local)
End Sub
Public Property TimeZone As TimeZoneInfo
Get
Return tz
End Get
Set
tz = Value
End Set
End Property
Public Property DateTime As Date
Get
Return dt
End Get
Set
dt = Value
End Set
End Property
End Structure
End Namespace
重要
構造体は DateWithTimeZone
次の 2 つの例で使用され、オブジェクトの DateWithTimeZone
配列をシリアル化および逆シリアル化します。 この記事のサンプル セット全体のソースは、GitHub のドキュメント リポジトリの Visual Basic、 F#、または C# で確認できます。
構造体を DateWithTimeZone
使用すると、日付と時刻をタイム ゾーン情報と共に保持できます。 次の例では、クラスを BinaryFormatter 使用してオブジェクトの DateWithTimeZone
配列をシリアル化します。
public static void SaveDateWithTimeZone()
{
DateWithTimeZone[] dates = { new DateWithTimeZone(new DateTime(2014, 8, 9, 19, 30, 0),
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")),
new DateWithTimeZone(new DateTime(2014, 8, 15, 19, 0, 0),
TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time")),
new DateWithTimeZone(new DateTime(2014, 8, 22, 19, 30, 0),
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")),
new DateWithTimeZone(new DateTime(2014, 8, 28, 19, 0, 0),
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")) };
var fs = new FileStream(@".\Schedule.bin", FileMode.Create);
var formatter = new BinaryFormatter();
try
{
formatter.Serialize(fs, dates);
// Display dates.
foreach (var date in dates)
{
TimeZoneInfo tz = date.TimeZone;
Console.WriteLine($"{date.DateTime} {(tz.IsDaylightSavingTime(date.DateTime) ? tz.DaylightName : tz.StandardName)}");
}
}
catch (SerializationException e)
{
Console.WriteLine($"Serialization failed. Reason: {e.Message}");
}
finally
{
if (fs != null) fs.Close();
}
}
// The example displays the following output:
// 8/9/2014 7:30:00 PM Eastern Daylight Time
// 8/15/2014 7:00:00 PM Pacific Daylight Time
// 8/22/2014 7:30:00 PM Eastern Daylight Time
// 8/28/2014 7:00:00 PM Eastern Daylight Time
// BinaryFormatter serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information.
let saveDateWithTimeZone () =
let dates =
[| DateWithTimeZone(DateTime(2014, 8, 9, 19, 30, 0),
TimeZoneInfo.FindSystemTimeZoneById "Eastern Standard Time")
DateWithTimeZone(DateTime(2014, 8, 15, 19, 0, 0),
TimeZoneInfo.FindSystemTimeZoneById "Pacific Standard Time")
DateWithTimeZone(DateTime(2014, 8, 22, 19, 30, 0),
TimeZoneInfo.FindSystemTimeZoneById "Eastern Standard Time")
DateWithTimeZone(DateTime(2014, 8, 28, 19, 0, 0),
TimeZoneInfo.FindSystemTimeZoneById "Eastern Standard Time") |]
use fs = new FileStream(@".\Schedule.bin", FileMode.Create)
let formatter = BinaryFormatter()
try
formatter.Serialize(fs, dates)
// Display dates.
for date in dates do
let tz = date.TimeZone
printfn $"{date.DateTime} {if tz.IsDaylightSavingTime date.DateTime then tz.DaylightName else tz.StandardName}"
with :? SerializationException as e ->
printfn $"Serialization failed. Reason: {e.Message}"
// The example displays the following output:
// 8/9/2014 7:30:00 PM Eastern Daylight Time
// 8/15/2014 7:00:00 PM Pacific Daylight Time
// 8/22/2014 7:30:00 PM Eastern Daylight Time
// 8/28/2014 7:00:00 PM Eastern Daylight Time
Public Sub SaveDateWithTimeZone()
Dim dates As DateWithTimeZone() = {New DateWithTimeZone(#8/9/2014 7:30PM#,
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")),
New DateWithTimeZone(#8/15/2014 7:00PM#,
TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time")),
New DateWithTimeZone(#8/22/2014 7:30PM#,
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")),
New DateWithTimeZone(#8/28/2014 7:00PM#,
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"))}
Dim fs As New FileStream(".\Schedule.bin", FileMode.Create)
Dim formatter As New BinaryFormatter()
Try
formatter.Serialize(fs, dates)
Catch e As SerializationException
Console.WriteLine($"Serialization failed. Reason: {e.Message}")
Finally
If fs IsNot Nothing Then fs.Close()
End Try
' Display dates.
For Each dateInfo In dates
Dim tz As TimeZoneInfo = dateInfo.TimeZone
Console.WriteLine($"{dateInfo.DateTime} {If(tz.IsDaylightSavingTime(dateInfo.DateTime), tz.DaylightName, tz.StandardName)}")
Next
End Sub
' The example displays the following output:
' 8/9/2014 7:30:00 PM Eastern Daylight Time
' 8/15/2014 7:00:00 PM Pacific Daylight Time
' 8/22/2014 7:30:00 PM Eastern Daylight Time
' 8/28/2014 7:00:00 PM Eastern Daylight Time
次の例では、メソッドを BinaryFormatter.Deserialize 呼び出して逆シリアル化します。
public static void RestoreDateWithTimeZone()
{
const string filename = @".\Schedule.bin";
FileStream fs;
if (File.Exists(filename))
{
fs = new FileStream(filename, FileMode.Open);
}
else
{
Console.WriteLine("Unable to find file to deserialize.");
return;
}
var formatter = new BinaryFormatter();
DateWithTimeZone[] dates;
try
{
dates = (DateWithTimeZone[])formatter.Deserialize(fs);
// Display dates.
foreach (var date in dates)
{
TimeZoneInfo tz = date.TimeZone;
Console.WriteLine($"{ date.DateTime} {(tz.IsDaylightSavingTime(date.DateTime) ? tz.DaylightName : tz.StandardName)}");
}
}
catch (SerializationException e)
{
Console.WriteLine($"Deserialization failed. Reason: {e.Message}");
}
finally
{
if (fs != null) fs.Close();
}
}
// The example displays the following output:
// 8/9/2014 7:30:00 PM Eastern Daylight Time
// 8/15/2014 7:00:00 PM Pacific Daylight Time
// 8/22/2014 7:30:00 PM Eastern Daylight Time
// 8/28/2014 7:00:00 PM Eastern Daylight Time
let restoreDateWithTimeZone () =
let filename = @".\Schedule.bin"
if File.Exists filename then
use fs = new FileStream(filename, FileMode.Open)
let formatter = BinaryFormatter()
try
let dates = formatter.Deserialize fs :?> DateWithTimeZone []
// Display dates.
for date in dates do
let tz = date.TimeZone
printfn $"{date.DateTime} {if tz.IsDaylightSavingTime date.DateTime then tz.DaylightName else tz.StandardName}"
with :? SerializationException as e ->
printfn $"Deserialization failed. Reason: {e.Message}"
else
printfn "Unable to find file to deserialize."
// The example displays the following output:
// 8/9/2014 7:30:00 PM Eastern Daylight Time
// 8/15/2014 7:00:00 PM Pacific Daylight Time
// 8/22/2014 7:30:00 PM Eastern Daylight Time
// 8/28/2014 7:00:00 PM Eastern Daylight Time
Public Sub RestoreDateWithTimeZone()
Dim fs As FileStream
If File.Exists(filename) Then
fs = New FileStream(filename, FileMode.Open)
Else
Console.WriteLine("Unable to find file to deserialize.")
Exit Sub
End If
Dim formatter As New BinaryFormatter()
Dim dates As DateWithTimeZone ()= Nothing
Try
dates = DirectCast(formatter.Deserialize(fs), DateWithTimeZone())
' Display dates.
For Each dateInfo In dates
Dim tz As TimeZoneInfo = dateInfo.TimeZone
Console.WriteLine($"{dateInfo.DateTime} {If(tz.IsDaylightSavingTime(dateInfo.DateTime), tz.DaylightName, tz.StandardName)}")
Next
Catch e As SerializationException
Console.WriteLine("Deserialization failed. Reason: {e.Message}")
Finally
If fs IsNot Nothing Then fs.Close()
End Try
End Sub
' The example displays the following output:
' 8/9/2014 7:30:00 PM Eastern Daylight Time
' 8/15/2014 7:00:00 PM Pacific Daylight Time
' 8/22/2014 7:30:00 PM Eastern Daylight Time
' 8/28/2014 7:00:00 PM Eastern Daylight Time
DateTime と TimeSpan
DateTime値の型とTimeSpanが異なるのは、a DateTime が一瞬の時間を表すのに対し、a 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 日です。 0001 年 1 月 1 日ではなく、午後 3 時に C.E. 午後 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) |
指定した月および年の日数を返します。 |
Equals(DateTime) |
このインスタンスの値が、指定した DateTime インスタンスの値と等しいかどうかを示す値を返します。 |
Equals(DateTime, DateTime) |
DateTime の 2 つのインスタンスが同じ日付/時刻値を持っているかどうかを示す値を返します。 |
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<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) |
指定した日付と時刻から指定した時間間隔を減算して、新しい日付と時刻を返します。 |
明示的なインターフェイスの実装
適用対象
スレッド セーフ
この型のすべてのメンバーはスレッド セーフです。 インスタンスの状態を変更するように見えるメンバーは、実際には新しい値で初期化された新しいインスタンスを返します。 他の型と同様に、この型のインスタンスを含む共有変数の読み取りと書き込みは、スレッドセーフを保証するためにロックによって保護する必要があります。