C#时间戳的获取与转换方法

/// <summary>
        /// 获取当前时间戳
        /// </summary>
        /// <param name="bflag"></param>
        /// <returns></returns>
        public static string GetTimeStamp()
        {
            return GetTimeStamp(DateTime.Now);
        }
        /// <summary>
        /// 获取指定时间戳
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string GetTimeStamp(DateTime date)
        {
            TimeSpan ts = date - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalSeconds).ToString(); ;
        }
        /// <summary>
        /// 将时间戳转换成DateTime
        /// </summary>
        /// <param name="timeStamp"></param>
        /// <returns></returns>
        private 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);
        }

 

posted @ 2015-11-27 15:21  Mr. Hu  阅读(459)  评论(0编辑  收藏  举报
Map