共用方式為


操作說明:反覆存取日期與時間值

在許多應用程式中,日期和時間值的用途都是要明確地識別單一時間點。 本文顯示如何儲存並還原 DateTime 值、DateTimeOffset 值,以及具有時區資訊的日期與時間值,讓還原值能夠識別出與儲存值相同的時間。

反覆存取 DateTime 值

  1. 搭配 "o" 格式規範呼叫 DateTime.ToString(String) 方法,以將 DateTime 值轉換為其字串表示。

  2. DateTime 值的字串表示儲存至檔案,或跨處理序、應用程式定義域或電腦界限進行傳遞。

  3. 擷取代表 DateTime 值的字串。

  4. 呼叫 DateTime.Parse(String, IFormatProvider, DateTimeStyles) 方法,然後傳遞 DateTimeStyles.RoundtripKind 作為 styles 參數值。

下列範例說明如何反覆存取 DateTime 值。

const string fileName = @".\DateFile.txt";

StreamWriter outFile = new StreamWriter(fileName);

// Save DateTime value.
DateTime dateToSave = DateTime.SpecifyKind(new DateTime(2008, 6, 12, 18, 45, 15),
                                           DateTimeKind.Local);
string? dateString = dateToSave.ToString("o");
Console.WriteLine("Converted {0} ({1}) to {2}.",
                  dateToSave.ToString(),
                  dateToSave.Kind.ToString(),
                  dateString);
outFile.WriteLine(dateString);
Console.WriteLine("Wrote {0} to {1}.", dateString, fileName);
outFile.Close();

// Restore DateTime value.
DateTime restoredDate;

using StreamReader inFile = new StreamReader(fileName);
dateString = inFile.ReadLine();

if (dateString is not null)
{
    restoredDate = DateTime.Parse(dateString, null, DateTimeStyles.RoundtripKind);
    Console.WriteLine("Read {0} ({2}) from {1}.", restoredDate.ToString(),
                                                  fileName,
                                                  restoredDate.Kind.ToString());
}

// The example displays the following output:
//    Converted 6/12/2008 6:45:15 PM (Local) to 2008-06-12T18:45:15.0000000-05:00.
//    Wrote 2008-06-12T18:45:15.0000000-05:00 to .\DateFile.txt.
//    Read 6/12/2008 6:45:15 PM (Local) from .\DateFile.txt.
Const fileName As String = ".\DateFile.txt"

Dim outFile As New StreamWriter(fileName)

' Save DateTime value.
Dim dateToSave As Date = DateTime.SpecifyKind(#06/12/2008 6:45:15 PM#, _
                                              DateTimeKind.Local)
Dim dateString As String = dateToSave.ToString("o")
Console.WriteLine("Converted {0} ({1}) to {2}.", dateToSave.ToString(), _
                  dateToSave.Kind.ToString(), dateString)
outFile.WriteLine(dateString)
Console.WriteLine("Wrote {0} to {1}.", dateString, fileName)
outFile.Close()

' Restore DateTime value.
Dim restoredDate As Date

Dim inFile As New StreamReader(fileName)
dateString = inFile.ReadLine()
inFile.Close()
restoredDate = DateTime.Parse(dateString, Nothing, DateTimeStyles.RoundTripKind)
Console.WriteLine("Read {0} ({2}) from {1}.", restoredDate.ToString(), _
                  fileName, restoredDAte.Kind.ToString())
' The example displays the following output:
'    Converted 6/12/2008 6:45:15 PM (Local) to 2008-06-12T18:45:15.0000000-05:00.
'    Wrote 2008-06-12T18:45:15.0000000-05:00 to .\DateFile.txt.
'    Read 6/12/2008 6:45:15 PM (Local) from .\DateFile.txt.

反覆存取 DateTime 值時,這項技術可以成功保留所有當地和全球通用時間的時間。 例如,如果本地 DateTime 值是儲存在美國太平洋標準時間時區系統中,並且還原為美國中央標準時間時區系統中,則還原的日期和時間會比原始時間多兩小時,反映出兩個時區之間的時差。 不過,針對未指定的時間,這項技術並不一定準確。 所有 Kind 屬性為 UnspecifiedDateTime 值,都會被視為當地時間。 若不是本地時間,則 DateTime 不會成功識別正確的時間點。 這項限制的因應措施,是將日期和時間值與其儲存和還原作業的時區緊密結合。

反覆存取 DateTimeOffset 值

  1. 搭配 "o" 格式規範呼叫 DateTimeOffset.ToString(String) 方法,以將 DateTimeOffset 值轉換為其字串表示。

  2. DateTimeOffset 值的字串表示儲存至檔案,或跨處理序、應用程式定義域或電腦界限進行傳遞。

  3. 擷取代表 DateTimeOffset 值的字串。

  4. 呼叫 DateTimeOffset.Parse(String, IFormatProvider, DateTimeStyles) 方法,然後傳遞 DateTimeStyles.RoundtripKind 作為 styles 參數值。

下列範例說明如何反覆存取 DateTimeOffset 值。

const string fileName = @".\DateOff.txt";

StreamWriter outFile = new StreamWriter(fileName);

// Save DateTime value.
DateTimeOffset dateToSave = new DateTimeOffset(2008, 6, 12, 18, 45, 15,
                                               new TimeSpan(7, 0, 0));
string? dateString = dateToSave.ToString("o");
Console.WriteLine("Converted {0} to {1}.", dateToSave.ToString(),
                  dateString);
outFile.WriteLine(dateString);
Console.WriteLine("Wrote {0} to {1}.", dateString, fileName);
outFile.Close();

// Restore DateTime value.
DateTimeOffset restoredDateOff;

using StreamReader inFile = new StreamReader(fileName);
dateString = inFile.ReadLine();

if (dateString is not null)
{
    restoredDateOff = DateTimeOffset.Parse(dateString, null,
                                           DateTimeStyles.RoundtripKind);
    Console.WriteLine("Read {0} from {1}.", restoredDateOff.ToString(), fileName);
}

// The example displays the following output:
//    Converted 6/12/2008 6:45:15 PM +07:00 to 2008-06-12T18:45:15.0000000+07:00.
//    Wrote 2008-06-12T18:45:15.0000000+07:00 to .\DateOff.txt.
//    Read 6/12/2008 6:45:15 PM +07:00 from .\DateOff.txt.
Const fileName As String = ".\DateOff.txt"

Dim outFile As New StreamWriter(fileName)

' Save DateTime value.
Dim dateToSave As New DateTimeOffset(2008, 6, 12, 18, 45, 15, _
                                     New TimeSpan(7, 0, 0))
Dim dateString As String = dateToSave.ToString("o")
Console.WriteLine("Converted {0} to {1}.", dateToSave.ToString(), dateString)
outFile.WriteLine(dateString)
Console.WriteLine("Wrote {0} to {1}.", dateString, fileName)
outFile.Close()

' Restore DateTime value.
Dim restoredDateOff As DateTimeOffset

Dim inFile As New StreamReader(fileName)
dateString = inFile.ReadLine()
inFile.Close()
restoredDateOff = DateTimeOffset.Parse(dateString, Nothing, DateTimeStyles.RoundTripKind)
Console.WriteLine("Read {0} from {1}.", restoredDateOff.ToString(), fileName)
' The example displays the following output:
'    Converted 6/12/2008 6:45:15 PM +07:00 to 2008-06-12T18:45:15.0000000+07:00.
'    Wrote 2008-06-12T18:45:15.0000000+07:00 to .\DateOff.txt.
'    Read 6/12/2008 6:45:15 PM +07:00 from .\DateOff.txt.

這項技術一律會明確地將 DateTimeOffset 值識別為單一時間點。 接著可呼叫 DateTimeOffset.ToUniversalTime 方法來將值轉換為國際標準時間 (UTC),或者可以呼叫 DateTimeOffset.ToOffsetTimeZoneInfo.ConvertTime(DateTimeOffset, TimeZoneInfo) 方法來將它轉換為特定時區的時間。 這項技術有個主要限制:在表示特定時區之時間的 DateTimeOffset 值上執行日期和時間算術時,可能不會產生該時區的精確結果。 這是因為在將 DateTimeOffset 值具現化時,就會解除它與其時區的關聯。 因此,當您執行日期和時間計算時,就無法再套用該時區的調整規則。 若要解決這個問題,您可以定義自訂類型,以包含日期與時間值及其隨附的時區。

編譯程式碼

這些範例需要以 C# using 指示詞或 Visual Basic Imports 陳述式匯入下列命名空間:

另請參閱