时间戳的转换

将DateTime类型的数据转换为时间戳

        #region 时间戳Timestamp
        /// <summary>
        /// 时间戳Timestamp  
        /// </summary>
        /// <returns></returns>
        public static int GetCreatetime()
        {
            DateTime DateStart = new DateTime(1970, 1, 1, 8, 0, 0);
            return Convert.ToInt32((DateTime.Now - DateStart).TotalSeconds);
        }

        public static int GetCreatetime(DateTime t)
        {
            DateTime DateStart = new DateTime(1970, 1, 1, 8, 0, 0);
            return Convert.ToInt32((t - DateStart).TotalSeconds);
        }
        #endregion

 

将时间戳转换为C#格式的时间

        #region 时间戳转为C#格式时间  
        /// <summary>  
        /// 时间戳转为C#格式时间  
        /// </summary>  
        /// <param name=”timeStamp”></param>  
        /// <returns></returns>  
        private static DateTime GetTime(string timeStamp)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(timeStamp + "0000000");
            TimeSpan toNow = new TimeSpan(lTime); return dtStart.Add(toNow);
        }
        #endregion

 

posted @ 2016-07-07 10:12  小菜鸟_code  阅读(226)  评论(0编辑  收藏  举报