共用方式為


DateTimeOffset.DateTime 屬性

定義

取得 DateTime 值,這個值表示目前 DateTimeOffset 物件的日期和時間。

public:
 property DateTime DateTime { DateTime get(); };
public DateTime DateTime { get; }
member this.DateTime : DateTime
Public ReadOnly Property DateTime As DateTime

屬性值

DateTime

目前 DateTimeOffset 物件的日期和時間。

範例

下列範例說明如何使用 DateTime 屬性,將 和 UtcNow 屬性傳 Now 回的時間轉換為 DateTime 值。

DateTimeOffset offsetDate;
DateTime regularDate;

offsetDate = DateTimeOffset.Now;
regularDate = offsetDate.DateTime;
Console.WriteLine("{0} converts to {1}, Kind {2}.",
                  offsetDate.ToString(),
                  regularDate,
                  regularDate.Kind);

offsetDate = DateTimeOffset.UtcNow;
regularDate = offsetDate.DateTime;
Console.WriteLine("{0} converts to {1}, Kind {2}.",
                  offsetDate.ToString(),
                  regularDate,
                  regularDate.Kind);
// If run on 3/6/2007 at 17:11, produces the following output:
//
//   3/6/2007 5:11:22 PM -08:00 converts to 3/6/2007 5:11:22 PM, Kind Unspecified.
//   3/7/2007 1:11:22 AM +00:00 converts to 3/7/2007 1:11:22 AM, Kind Unspecified.
let offsetDate = DateTimeOffset.Now
let regularDate = offsetDate.DateTime
printfn $"{offsetDate} converts to {regularDate}, Kind {regularDate.Kind}."

let offsetDate = DateTimeOffset.UtcNow
let regularDate = offsetDate.DateTime
printfn $"{offsetDate} converts to {regularDate}, Kind {regularDate.Kind}."

// If run on 3/6/2007 at 17:11, produces the following output:
//
//   3/6/2007 5:11:22 PM -08:00 converts to 3/6/2007 5:11:22 PM, Kind Unspecified.
//   3/7/2007 1:11:22 AM +00:00 converts to 3/7/2007 1:11:22 AM, Kind Unspecified.
Dim offsetDate As DateTimeOffset 
Dim regularDate As Date

offsetDate = DateTimeOffset.Now
regularDate = offsetDate.DateTime
Console.WriteLine("{0} converts to {1}, Kind {2}.", _
                  offsetDate.ToString(), _
                  regularDate, _ 
                  regularDate.Kind)
               
offsetDate = DateTimeOffset.UtcNow
regularDate = offsetDate.DateTime
Console.WriteLine("{0} converts to {1}, Kind {2}.", _
                  offsetDate.ToString(), _
                  regularDate, _
                  regularDate.Kind)
' If run on 3/6/2007 at 17:11, produces the following output:
'
'   3/6/2007 5:11:22 PM -08:00 converts to 3/6/2007 5:11:22 PM, Kind Unspecified.
'   3/7/2007 1:11:22 AM +00:00 converts to 3/7/2007 1:11:22 AM, Kind Unspecified.

備註

屬性 DateTime 不會受到 屬性的值 Offset 影響。

這個屬性會傳回 物件的日期和時間元件 DateTimeOffset ,這讓執行轉換 DateTime 很有用 DateTimeOffset 。 例如,如果 DateTimeOffset 物件具有 「1/12/07 4:01pm + 7:30」 的日期和時間值,則屬性會傳回日期 「1/12/07 4:01pm」。

DateTime.Kind傳回 DateTime 物件的 屬性值為 DateTimeKind.Unspecified

適用於

另請參閱