C# 的時(shí)間戳轉(zhuǎn)換今天有時(shí)間戳轉(zhuǎn)換的需求,網(wǎng)上找了半天才找到相關(guān)代碼,經(jīng)測(cè)試有效,特作此筆記和大家分享! 1.時(shí)間戳轉(zhuǎn)為C#格式時(shí)間 /// <summary> /// 時(shí)間戳轉(zhuǎn)為C#格式時(shí)間 /// </summary> /// <param name="timeStamp">Unix時(shí)間戳格式</param> /// <returns>C#格式時(shí)間</returns> public 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); } 2.DateTime時(shí)間格式轉(zhuǎn)換為Unix時(shí)間戳格式 /// <summary> /// DateTime時(shí)間格式轉(zhuǎn)換為Unix時(shí)間戳格式 /// </summary> /// <param name="time"> DateTime時(shí)間格式</param> /// <returns>Unix時(shí)間戳格式</returns> public static int ConvertDateTimeInt(System.DateTime time) { System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); return (int)(time - startTime).TotalSeconds; } |
|
來(lái)自: 賈朋亮博客 > 《c#基礎(chǔ)》