如何在C#中获取Unix时间戳

本文翻译自: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;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值