日期类型转换时间戳及时间戳转时间类型

青春壹個敷衍的年華 提交于 2019-12-20 21:42:22
 

 

//日期转时间戳  
      public static long DateTimeToUnixTimestamp(DateTime dateTime)
        {
            return (dateTime.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
        }

//时间戳转日期
public static DateTime ToDateTime(this string timestamp)
{
        if(string.IsNullOrEmpty(timestamp))
       {
        throw new ArgumentNullException(timestamp);
       }
        DateTime dateTimeStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970,1,1));
    long lTime = long.Parse(timestamp + "0000000");
    TimeSpan toNow = new TimeSpan(lTime);
    return dateTimeStart.Add(toNow);
}

 

 

/// <summary>
/// unix时间戳转换成日期
/// </summary>
/// <param name="unixTimeStamp">时间戳(秒)</param>
/// <returns></returns>
public static DateTime UnixTimestampToDateTime(this DateTime target, long timestamp)
{
    var start = new DateTime(1970, 1, 1, 0, 0, 0, target.Kind);
    return start.AddSeconds(timestamp);
}

        /// <summary>
        /// unix时间戳转换成日期
        /// </summary>
        /// <param name="unixTimeStamp">时间戳(秒)</param>
        /// <returns></returns>
        public static DateTime UnixTimestampToDateTime(long timestamp)
        {
            var start = new DateTime(1970, 1, 1, 8, 0, 0, DateTimeKind.Unspecified);
            return start.AddMilliseconds(timestamp);
        }  

 

 版本号相关操作
 --/// <summary>
        --/// 已运行的最大行版本号
        --/// </summary>
        --public static byte[] RunMaxVersionNum = { 0x0 };
          --var nowMaxVersionNum = channelCommDal.GetMaxVersionNum(dbConnection);
          --        public byte[] GetMaxVersionNum(IDbConnection dbConnection)
    --    {
    --        return DbHelper.Default.QueryScalar<byte[]>(dbConnection,
    --            "SELECT MAX(VersionNum) FROM dbo.Fct_ChannelCommodity");
    --    }

           --var runVersionNum = "0x" + BitConverter.ToString(RunMaxVersionNum).Replace("-", "");

        --public static bool ByteEquals(byte[] b1, byte[] b2)
        --{
        --    if (b1 == null && b2 == null)
        --    {
        --        return true;
        --    }
        --    if (b1 == null || b2 == null)
        --        return false;
        --    if (b1.Length != b2.Length)
        --        return false;
        --    return !b1.Where((t, i) => t != b2[i]).Any();
        --}
        
        --if (ByteEquals(nowMaxVersionNum, RunMaxVersionNum))
        
        -- Version = Math.Abs(Guid.NewGuid().GetHashCode()).ToString(),   --nvarchar(64)

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!