共用方式為


DateTimeOffset.UtcNow 屬性

定義

取得 DateTimeOffset 一個日期與時間設定為當前協調世界時(UTC)日期與時間,且偏移量為 Zero的物件。

public:
 static property DateTimeOffset UtcNow { DateTimeOffset get(); };
public static DateTimeOffset UtcNow { get; }
static member UtcNow : DateTimeOffset
Public Shared ReadOnly Property UtcNow As DateTimeOffset

屬性值

一個日期和時間為當前協調世界時(UTC)且偏移量為 Zero的物件。

範例

以下範例說明協調世界時(UTC)與當地時間之間的關係。

DateTimeOffset localTime = DateTimeOffset.Now;
DateTimeOffset utcTime = DateTimeOffset.UtcNow;

Console.WriteLine("Local Time:          {0}", localTime.ToString("T"));
Console.WriteLine("Difference from UTC: {0}", localTime.Offset.ToString());
Console.WriteLine("UTC:                 {0}", utcTime.ToString("T"));
// If run on a particular date at 1:19 PM, the example produces
// the following output:
//    Local Time:          1:19:43 PM
//    Difference from UTC: -07:00:00
//    UTC:                 8:19:43 PM
let localTime = DateTimeOffset.Now
let utcTime = DateTimeOffset.UtcNow

printfn $"Local Time:          {localTime:T}"
printfn $"Difference from UTC: {localTime.Offset}"
printfn $"UTC:                 {utcTime:T}"

// If run on a particular date at 1:19 PM, the example produces
// the following output:
//    Local Time:          1:19:43 PM
//    Difference from UTC: -07:00:00
//    UTC:                 8:19:43 PM
Dim localTime As DateTimeOffset = DateTimeOffset.Now
Dim utcTime As DateTimeOffset = DateTimeOffset.UtcNow

Console.WriteLine("Local Time:          {0}", localTime.ToString("T"))
Console.WriteLine("Difference from UTC: {0}", localTime.Offset.ToString())
Console.WriteLine("UTC:                 {0}", utcTime.ToString("T"))
' If run on a particular date at 1:19 PM, the example produces
' the following output:
'    Local Time:          1:19:43 PM
'    Difference from UTC: -07:00:00
'    UTC:                 8:19:43 PM

備註

UtcNow 性質根據當地系統的時鐘時間及當地系統時區定義的偏移量,計算出當前的通用協調時間(UTC)。

當前UTC時間毫秒分量的精度取決於系統時鐘的解析度。 在 Windows NT 3.5 及以後版本,以及 Windows Vista 作業系統中,時鐘的解析度約為 10-15 毫秒。

適用於

另請參閱