DateTimeOffset.UtcNow 屬性

定義

取得 DateTimeOffset 物件,這個物件的日期和時間已設定為目前 Coordinated Universal Time (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

屬性值

其日期和時間為目前 Coordinated Universal Time (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 毫秒。

適用於

另請參閱