本文翻译自:How to get the unix timestamp in C#
我曾经看过stackoverflow,甚至看过一些建议的问题,但似乎都没有答案,如何在C#中获得unix时间戳?
#1楼
参考:https://stackoom.com/question/1Bz2W/如何在C-中获取Unix时间戳
#2楼
You get a unix timestamp in C# by using DateTime.UtcNow
and subtracting the epoch time of 1970-01-01. 通过使用DateTime.UtcNow
并减去1970-01-01的纪元时间,可以在C#中获得unix时间戳。
eg 例如
Int32 unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
DateTime.UtcNow
can be replaced with any DateTime
object that you would like to get the unix timestamp for. DateTime.UtcNow
可以替换为您要获取Unix时间戳的任何DateTime
对象。
There is also a field, DateTime.UnixEpoch
, which is very poorly documented by MSFT, but may be a substitute for new DateTime(1970, 1, 1)
还有一个字段DateTime.UnixEpoch
,由MSFT 记录得很差 ,但是可以替代new DateTime(1970, 1, 1)
#3楼
This is what I use. 这就是我用的。
public class TimeStamp
{
public Int32 UnixTimeStampUTC()
{
Int32 unixTimeStamp;
DateTime currentTime = DateTime.Now;
DateTime zuluTime = currentTime.ToUniversalTime();
DateTime unixEpoch = new DateTime(1970, 1, 1);
unixTimeStamp = (Int32)(zuluTime.Subtract(unixEpoch)).TotalSeconds;
return unixTimeStamp;
}
}
#4楼
This is what I use: 这是我用的:
public long UnixTimeNow()
{
var timeSpan = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
return (long)timeSpan.TotalSeconds;
}
Keep in mind that this method will return the time as Coordinated Univeral Time (UTC). 请记住,此方法将以协调世界时(UTC)返回时间。
#5楼
Truncating .TotalSeconds
is important since it's defined as the value of the current System.TimeSpan structure expressed in whole fractional seconds.
截断.TotalSeconds
很重要,因为它被定义为the value of the current System.TimeSpan structure expressed in whole fractional seconds.
And how about an extension for DateTime
? 以及DateTime
的扩展如何? The second one is probably more confusing that it's worth until property extensions exist. 在存在属性扩展之前,第二个可能更令人困惑,它值得。
/// <summary>
/// Converts a given DateTime into a Unix timestamp
/// </summary>
/// <param name="value">Any DateTime</param>
/// <returns>The given DateTime in Unix timestamp format</returns>
public static int ToUnixTimestamp(this DateTime value)
{
return (int)Math.Truncate((value.ToUniversalTime().Subtract(new DateTime(1970, 1, 1))).TotalSeconds);
}
/// <summary>
/// Gets a Unix timestamp representing the current moment
/// </summary>
/// <param name="ignored">Parameter ignored</param>
/// <returns>Now expressed as a Unix timestamp</returns>
public static int UnixTimestamp(this DateTime ignored)
{
return (int)Math.Truncate((DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds);
}
#6楼
You can also use Ticks. 您也可以使用Ticks。 I'm coding for Windows Mobile so don't have the full set of methods. 我正在为Windows Mobile编写代码,所以没有完整的方法集。 TotalSeconds is not available to me. 我无法使用TotalSeconds 。
long epochTicks = new DateTime(1970, 1, 1).Ticks;
long unixTime = ((DateTime.UtcNow.Ticks - epochTicks) / TimeSpan.TicksPerSecond);
or 要么
TimeSpan epochTicks = new TimeSpan(new DateTime(1970, 1, 1).Ticks);
TimeSpan unixTicks = new TimeSpan(DateTime.UtcNow.Ticks) - epochTicks;
double unixTime = unixTicks.TotalSeconds;