DateTimeOffset.UtcNow Egenskap

Definition

Hämtar ett DateTimeOffset objekt vars datum och tid är inställda på aktuellt utc-datum och tid (Coordinated Universal Time) och vars förskjutning är Zero.

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

Egenskapsvärde

Ett objekt vars datum och tid är den aktuella koordinerade universella tiden (UTC) och vars förskjutning är Zero.

Exempel

I följande exempel visas relationen mellan UTC (Coordinated Universal Time) och lokal tid.

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

Kommentarer

Egenskapen UtcNow beräknar den aktuella universalkoordinerade tiden (UTC) baserat på det lokala systemets klocktid och en förskjutning som definieras av det lokala systemets tidszon.

Precisionen för den aktuella UTC-tidens millisekunders komponent beror på systemklockans upplösning. På Windows NT 3.5 och senare och Windows Vista operativsystem är klockans upplösning cirka 10–15 millisekunder.

Gäller för

Se även