C# 时间与时间戳互转 13位

/// <summary>  
        /// 将c# DateTime时间格式转换为Unix时间戳格式  
        /// </summary>  
        /// <param name="time">时间</param>  
        /// <returns>long</returns>  
        public static long ConvertDateTimeToInt(System.DateTime time)  
        {          
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));          
            long t = (time.Ticks - startTime.Ticks) / 10000;   //除10000调整为13位      
            return t;  
        }  
        /// <summary>        
        /// 时间戳转为C#格式时间        
        /// </summary>        
        /// <param name=”timeStamp”></param>        
        /// <returns></returns>        
        private DateTime ConvertStringToDateTime(string timeStamp)        
        {            
             DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));            
             long lTime = long.Parse(timeStamp + "0000");            
             TimeSpan toNow = new TimeSpan(lTime); 
             return dtStart.Add(toNow);        
        } 

 

posted on 2015-11-29 19:58  静以修身俭以养德  阅读(359)  评论(0编辑  收藏  举报

导航