国产精品人成在线播放新网站,欧洲吸奶大片在线看,亚洲欧洲成人a∨在线,国产18禁黄网站免费观看

微信號(hào):
當(dāng)前位置:首頁(yè) > 資訊文摘 > 新聞資訊

時(shí)間戳幫助類

2018/1/13 14:39:12

時(shí)間戳幫助類

/// <summary>
    /// 時(shí)間戳
    /// </summary>
    public class TimeHelp
    {
        /// <summary>
        /// 獲取時(shí)間戳
        /// </summary>
        /// <returns></returns>
        public static string GetTimeStamp(System.DateTime time,int length=13)
        {
            long ts = ConvertDateTimeToInt(time);
            return ts.ToString().Substring(0,length);
        }
        /// <summary>  
        /// 將c# DateTime時(shí)間格式轉(zhuǎn)換為Unix時(shí)間戳格式  
        /// </summary>  
        /// <param name="time">時(shí)間</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調(diào)整為13位      
            return t;
        }
        /// <summary>        
        /// 時(shí)間戳轉(zhuǎn)為C#格式時(shí)間        
        /// </summary>        
        /// <param name=”timeStamp”></param>        
        /// <returns></returns>        
        public static 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);
        }

        /// <summary>
        /// 時(shí)間戳轉(zhuǎn)為C#格式時(shí)間10位
        /// </summary>
        /// <param name="timeStamp">Unix時(shí)間戳格式</param>
        /// <returns>C#格式時(shí)間</returns>
        public static DateTime GetDateTimeFrom1970Ticks(long curSeconds)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            return dtStart.AddSeconds(curSeconds);
        }

        /// <summary>
        /// 驗(yàn)證時(shí)間戳
        /// </summary>
        /// <param name="time"></param>
        /// <param name="interval">差值(分鐘)</param>
        /// <returns></returns>
        public static bool IsTime(long time, double interval)
        {
            DateTime dt = GetDateTimeFrom1970Ticks(time);
            //取現(xiàn)在時(shí)間
            DateTime dt1 = DateTime.Now.AddMinutes(interval);
            DateTime dt2 = DateTime.Now.AddMinutes(interval * -1);
            if (dt > dt2 && dt < dt1)
            {
                return true;
            }
            else {
                return false;
            }
        }

        /// <summary>
        /// 判斷時(shí)間戳是否正確(驗(yàn)證前8位)
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static bool IsTime(string time)
        {
            string str = GetTimeStamp(DateTime.Now,8);
            if (str.Equals(time))
            {
                return true;
            }
            else {
                return false;
            }
        }
    }

相關(guān)新聞: