简而言之,DateTimeOffset是一种瞬时时间(即绝对时间),DateTime是一种历法时间(即钟表时间)。 最后,这两个数据类型都有Now和UtcNow两个方法。Now用于获取现在的时间,UtcNow获取Utc的现在时间。而实际上,Now内部是先调用UtcNow,然后进行转换得到。所以,在一般情况下,都应该使用UtcNow。 获取时间戳new DateTimeOffset(Dat...
简而言之,DateTimeOffset是一种瞬时时间(即绝对时间),DateTime是一种历法时间(即钟表时间)。 最后,这两个数据类型都有Now和UtcNow两个方法。Now用于获取现在的时间,UtcNow获取Utc的现在时间。而实际上,Now内部是先调用UtcNow,然后进行转换得到。所以,在一般情况下,都应该使用UtcNow。 在文章结束的时候,给大家留个小...
DateTime localNow = DateTime.Now; DateTimeOffset localOffset = new DateTimeOffset(localNow); Console.WriteLine(localOffset.ToString()); DateTime utcNow = DateTime.UtcNow; DateTimeOffset utcOffset = new DateTimeOffset(utcNow); Console.WriteLine(utcOffset.ToString()); DateTime unspecifiedNow = DateTime....
DateTime.UtcNow and DateTimeOffset.UtcNow are 2.5x slower compared to 2.2 [Benchmark] public DateTimeOffset GetUtcNow() => DateTimeOffset.UtcNow; [Benchmark] public DateTime GetUtcNow() => DateTime.UtcNow; Repro git clone https://github...
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"));...
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"));...
DateTimeOffset thisDate;// Show output for UTC timethisDate = DateTimeOffset.UtcNow; Console.WriteLine(thisDate.ToString());// Displays 3/28/2007 7:13:50 PM +00:00// Show output for local timethisDate = DateTimeOffset.Now; Console.WriteLine(thisDate.ToString());// Displays 3/28/2007 12...
取得DateTimeOffset 物件,這個物件的日期和時間已設定為目前 Coordinated Universal Time (UTC) 日期和時間,且位移為 Zero。
获取一个 DateTimeOffset 对象,其日期和时间设置为当前的协调世界时 (UTC) 日期和时间,其偏移量为 Zero。
Gets a DateTimeOffset object whose date and time are set to the current Coordinated Universal Time (UTC) date and time and whose offset is TimeSpan.Zero. Namespace: System Assembly: mscorlib (in mscorlib.dll) Syntax C# 复制 public static DateTimeOffset UtcNow { get; } Property Value...