.NET框架是否支持基于UNIX时间戳以秒为单位获取当前时间?

use*_*414 9 .net c#

我想知道.NET是否包含将当前时间(以秒或毫秒为单位)转换为UNIX时间戳(偏离1970/1/1)的方法?

Mat*_*ing 32

TimeSpan t = (DateTime.UtcNow - new DateTime(1970, 1, 1));
Console.WriteLine((int)t.TotalSeconds);
Run Code Online (Sandbox Code Playgroud)


小智 11

你可以得到1970年的时间戳(即UNIX时间戳),如下所示:

TimeSpan timeSpan = DateTime.UtcNow - new DateTime(1970,1,1,0,0,0);
double unixVersion = timeSpan.TotalSeconds;
Run Code Online (Sandbox Code Playgroud)