这就是你需要的:
public static DateTime UnixTimeStampToDateTime( double unixTimeStamp ) { // Unix timestamp is seconds past epoch System.DateTime dtDateTime = new DateTime(1970,1,1,0,0,0,0,System.DateTimeKind.Utc); dtDateTime = dtDateTime.AddSeconds( unixTimeStamp ).ToLocalTime(); return dtDateTime; }
或者,对于Java(由于时间戳以毫秒为单位而不是秒,因此不同):
public static DateTime JavaTimeStampToDateTime( double javaTimeStamp ) { // Java timestamp is milliseconds past epoch System.DateTime dtDateTime = new DateTime(1970,1,1,0,0,0,0,System.DateTimeKind.Utc); dtDateTime = dtDateTime.AddMilliseconds( javaTimeStamp ).ToLocalTime(); return dtDateTime; }
在.NET(V4.6)的最新版本,增加了对Unix时间转换的内置支持.这包括来自Unix时间和来自Unix或毫秒的时间.
以秒为单位的Unix时间DateTimeOffset
:
DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeSeconds(1000);
DateTimeOffset
以秒为单位的Unix时间:
long unixTimeStampInSeconds = dateTimeOffset.ToUnixTimeSeconds();
Unix时间的毫秒时间(以毫秒为单位)DateTimeOffset
:
DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeMilliseconds(1000000);
DateTimeOffset
以毫秒为单位的Unix时间:
long unixTimeStampInMilliseconds = dateTimeOffset.ToUnixTimeMilliseconds();
注意:这些方法与UTC进行转换DateTimeOffset
.要获得DateTime
表示,只需使用DateTimeOffset.UtcDateTime
或DateTimeOffset.LocalDateTime
属性:
DateTime dateTime = dateTimeOffset.UtcDateTime;
DateTime到UNIX时间戳:
public static double DateTimeToUnixTimestamp(DateTime dateTime) { return (TimeZoneInfo.ConvertTimeToUtc(dateTime) - new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc)).TotalSeconds; }
"UTC不会随着季节的变化而变化,但如果时区管辖区域遵守夏令时(夏令时),则当地时间或民用时间可能会发生变化.例如,UTC提前5小时(即当天晚些时候)比当地时间在美国东海岸的冬季,但提前4小时,在那里观察到夏令时."
所以这是我的代码:
TimeSpan span = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0,DateTimeKind.Utc)); double unixTime = span.TotalSeconds;
如果您需要高于毫秒的精度,请注意!
.NET(v4.6)方法(例如FromUnixTimeMilliseconds)不提供此精度.
AddSeconds和AddMilliseconds也会切断double中的微秒.
这些版本具有高精度:
Unix - > DateTime
public static DateTime UnixTimestampToDateTime(double unixTime) { DateTime unixStart = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc); long unixTimeStampInTicks = (long) (unixTime * TimeSpan.TicksPerSecond); return new DateTime(unixStart.Ticks + unixTimeStampInTicks, System.DateTimeKind.Utc); }
DateTime - > Unix
public static double DateTimeToUnixTimestamp(DateTime dateTime) { DateTime unixStart = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc); long unixTimeStampInTicks = (dateTime.ToUniversalTime() - unixStart).Ticks; return (double) unixTimeStampInTicks / TimeSpan.TicksPerSecond; }
请参见IdentityModel.EpochTimeExtensions
public static class EpochTimeExtensions { ////// Converts the given date value to epoch time. /// public static long ToEpochTime(this DateTime dateTime) { var date = dateTime.ToUniversalTime(); var ticks = date.Ticks - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).Ticks; var ts = ticks / TimeSpan.TicksPerSecond; return ts; } ////// Converts the given date value to epoch time. /// public static long ToEpochTime(this DateTimeOffset dateTime) { var date = dateTime.ToUniversalTime(); var ticks = date.Ticks - new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero).Ticks; var ts = ticks / TimeSpan.TicksPerSecond; return ts; } ////// Converts the given epoch time to a public static DateTime ToDateTimeFromEpoch(this long intDate) { var timeInTicks = intDate * TimeSpan.TicksPerSecond; return new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddTicks(timeInTicks); } ///with kind. /// /// Converts the given epoch time to a UTC public static DateTimeOffset ToDateTimeOffsetFromEpoch(this long intDate) { var timeInTicks = intDate * TimeSpan.TicksPerSecond; return new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero).AddTicks(timeInTicks); } }. ///
为了补充ScottCher的答案,我最近发现自己处于令人烦恼的情况,即在输入数据集中将UNIX时间戳任意混合在一起的秒和毫秒.以下代码似乎处理得很好:
static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); static readonly double MaxUnixSeconds = (DateTime.MaxValue - UnixEpoch).TotalSeconds; public static DateTime UnixTimeStampToDateTime(double unixTimeStamp) { return unixTimeStamp > MaxUnixSeconds ? UnixEpoch.AddMilliseconds(unixTimeStamp) : UnixEpoch.AddSeconds(unixTimeStamp); }
Unix时间转换是.NET Framework 4.6中的新增功能.
您现在可以更轻松地将日期和时间值转换为.NET Framework类型和Unix时间.例如,在JavaScript客户端和.NET服务器之间转换时间值时,这可能是必要的.DateTimeOffset结构中添加了以下API :
static DateTimeOffset FromUnixTimeSeconds(long seconds) static DateTimeOffset FromUnixTimeMilliseconds(long milliseconds) long DateTimeOffset.ToUnixTimeSeconds() long DateTimeOffset.ToUnixTimeMilliseconds()
我只是通过将转换率与当地时间调整的1/1/1970进行比较,找到了正确的答案;
DateTime date = new DateTime(2011, 4, 1, 12, 0, 0, 0); DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0); TimeSpan span = (date - epoch); double unixTime =span.TotalSeconds;