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 毫秒。

适用于

另请参阅