zoukankan      html  css  js  c++  java
  • C# 将日期转换为时间戳(日期与时间戳互转)

    public static class AbpExtensions
    {
        /// <summary>
        /// 返回10位时间戳 Timestamp
        /// </summary>
        /// <param name="target"></param>
        /// <returns></returns>
        public static int ToUnixTimestamp(this DateTime target)
        {
            if (target.Kind == DateTimeKind.Unspecified)
                target = target.ToLocalTime();
            return (int)((target.ToUniversalTime().Ticks - 621355968000000000) / 10000000);
        }
    
        /// <summary>
        /// 将10位时间戳Timestamp转换成日期
        /// </summary>
        /// <param name="target"></param>
        /// <returns></returns>
        public static DateTime ToLocalDateTime(this int target)
        {
            var date = new DateTime(621355968000000000 + (long)target * (long)10000000, DateTimeKind.Utc);
            return date.ToLocalTime();
        }
    }

    上面两个静态方法已经被封装为扩展方法,可以在 DateTime 类型和 int 类型通过 “.” 调用。

    时间戳词条说明:

      时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。通俗的讲, 时间戳是一份能够表示一份数据在一个特定时间点已经存在的完整的可验证的数据。 它的提出主要是为用户提供一份电子证据, 以证明用户的某些数据的产生时间。 在实际应用上, 它可以使用在包括电子商务、 金融活动的各个方面, 尤其可以用来支撑公开密钥基础设施的 “不可否认” 服务。

    各种语言间相互转换请参见:http://www.matools.com/timestamp

  • 相关阅读:
    iSCSI又称为IPSAN
    文档类型定义DTD
    HDU 2971 Tower
    HDU 1588 Gauss Fibonacci
    URAL 1005 Stone Pile
    URAL 1003 Parity
    URAL 1002 Phone Numbers
    URAL 1007 Code Words
    HDU 3306 Another kind of Fibonacci
    FZU 1683 纪念SlingShot
  • 原文地址:https://www.cnblogs.com/fxck/p/13076750.html
Copyright © 2011-2022 走看看