class TimeCount<br /> {<br /> // 臨時變數,存放當前類能表示的最大年份值<br /> private static ulong MaxYear = 0;</p><p> /// <summary><br /> /// 擷取毫秒能表示的最大年份數<br /> /// </summary><br /> /// <returns>年份數最大值</returns><br /> public static ulong GetMaxYearCount()<br /> {<br /> if (TimeCount.MaxYear != 0)<br /> return TimeCount.MaxYear;<br /> else<br /> {<br /> double ulongMax = Math.Pow(2, 64);<br /> double yearToMiliseconds = (double)12 * (double)30 * (double)24 * (double)3600 * (double)1000;<br /> TimeCount.MaxYear = (ulong)(ulongMax / yearToMiliseconds);<br /> return TimeCount.MaxYear;<br /> }<br /> }</p><p> /// <summary><br /> /// 把當前系統時間轉換成毫秒<br /> /// </summary><br /> /// <returns>當前系統時間所對應的毫秒數</returns><br /> public static ulong GetCurrentTimeByMiliSec()<br /> {<br /> DateTime t = DateTime.Now;<br /> ulong millisecond = ((((((ulong)t.Year * 12 + (ulong)t.Month) * 30 + (ulong)t.Day) * 24 + (ulong)t.Hour) * 60 + (ulong)t.Minute) * 60<br /> + (ulong)t.Second) * 1000 + (ulong)(t.Millisecond);<br /> return millisecond;<br /> }</p><p> /// <summary><br /> /// 不太實用的函數,給定三個輸入,得到時間差,以字串形式返回<br /> /// </summary><br /> /// <param name="str">時間前面的首碼,例如“繪製XX所用時間為:”</param><br /> /// <param name="a">第一個時間</param><br /> /// <param name="b">第二個時間,無所謂先後,總之最後會取絕對值</param><br /> /// <returns></returns><br /> public static string GetTimeInterval(string str, ulong a, ulong b)<br /> {<br /> ulong interval = b - a;<br /> str += Convert.ToString(Math.Abs(( ((double)interval)/1000)));<br /> str += "秒";<br /> return str;<br /> }</p><p> /// <summary><br /> /// 得到兩個給定時間(毫秒)之間的時間差,最終值會去絕對值<br /> /// </summary><br /> /// <param name="a">早一些的時間(毫秒)</param><br /> /// <param name="b">晚一些的時間(毫秒)</param><br /> /// <returns></returns><br /> public static double GetTimeInterValBySec(ulong a, ulong b)<br /> {<br /> return Math.Abs(((double)b - (double)a)/(double)1000);<br /> }</p><p> /// <summary><br /> /// 小數點後保留一位小數<br /> /// </summary><br /> /// <param name="double1">一個小數</param><br /> /// <returns>一個字串</returns><br /> public static string RemainOneFigureAfterDot(double double1)<br /> {<br /> string tempStr = double1.ToString();<br /> string double1str;<br /> if (tempStr == "")<br /> {<br /> return "0.0";<br /> }<br /> int index = tempStr.IndexOf(".");<br /> if (index == -1)<br /> {<br /> double1str = tempStr + ".0";<br /> return double1str;<br /> }<br /> else<br /> {<br /> if (tempStr.Length < index + 2)<br /> return tempStr + ".0";<br /> else<br /> {<br /> double1str = tempStr.Substring(0, index + 2);<br /> return double1str;<br /> }<br /> }<br /> }</p><p> /// <summary><br /> /// 年月日時分化成秒<br /> /// </summary><br /> public enum YMDHmS<br /> {<br /> yearSc = 365 * 24 * 3600,<br /> monSc = 30 * 24 * 3600,<br /> daySc = 24 * 3600,<br /> houSc = 3600,<br /> minSc = 60<br /> }</p><p> /// <summary><br /> /// 把一個String格式的以秒為單位的時間,轉化成年月日時分秒為單位的時間字串,並最後的秒保留一位小數<br /> /// </summary><br /> /// <param name="seconds">秒數,string格式</param><br /> /// <returns></returns><br /> public static string SecondsToYYMMDDhhmmss(string seconds)<br /> {<br /> double secondsDble = Convert.ToDouble(seconds);<br /> return TimeCount.SecondsToYYMMDDhhmmss(secondsDble);<br /> }</p><p> /// <summary><br /> /// 把一個int格式的以秒為單位的時間,轉化成年月日時分秒為單位的時間字串,並最後的秒保留一位小數<br /> /// </summary><br /> /// <param name="seconds">秒數,int格式</param><br /> /// <returns></returns><br /> public static string SecondsToYYMMDDhhmmss(int seconds)<br /> {<br /> double secondsDble = Convert.ToDouble(seconds);<br /> return TimeCount.SecondsToYYMMDDhhmmss(secondsDble);<br /> }</p><p> /// <summary><br /> /// 把一個ulong格式的以秒為單位的時間,轉化成年月日時分秒為單位的時間字串,並最後的秒保留一位小數<br /> /// </summary><br /> /// <param name="seconds">秒數,ulong格式</param><br /> /// <returns></returns><br /> public static string SecondsToYYMMDDhhmmss(ulong seconds)<br /> {<br /> double secondsDble = Convert.ToDouble(seconds);<br /> return TimeCount.SecondsToYYMMDDhhmmss(secondsDble);<br /> }</p><p> /// <summary><br /> /// 把一個double格式的以秒為單位的時間,轉化成年月日時分秒為單位的時間字串,並最後的秒保留一位小數<br /> /// </summary><br /> /// <param name="secondsDble">秒數,double格式</param><br /> /// <returns></returns><br /> public static string SecondsToYYMMDDhhmmss(double secondsDble)<br /> {<br /> string returnSTR = "";<br /> if (secondsDble == 0)<br /> {<br /> return "0.0秒";<br /> }<br /> int YY = 0, MM = 0, DD = 0, HH = 0, mm = 0;<br /> if (secondsDble >= (uint)YMDHmS.yearSc)//年<br /> {<br /> while (secondsDble > 0)<br /> {<br /> secondsDble -= (uint)YMDHmS.yearSc;<br /> YY++;<br /> }<br /> secondsDble += (uint)YMDHmS.yearSc;<br /> YY--;<br /> }<br /> if (secondsDble >= (uint)YMDHmS.monSc)//月<br /> {<br /> while (secondsDble > 0)<br /> {<br /> secondsDble -= (uint)YMDHmS.monSc;<br /> MM++;<br /> }<br /> secondsDble += (uint)YMDHmS.monSc;<br /> MM--;<br /> }<br /> if (secondsDble >= (uint)YMDHmS.daySc)//日<br /> {<br /> while (secondsDble > 0)<br /> {<br /> secondsDble -= (uint)YMDHmS.daySc;<br /> DD++;<br /> }<br /> secondsDble += (uint)YMDHmS.daySc;<br /> DD--;<br /> }<br /> if (secondsDble >= (uint)YMDHmS.houSc)//時<br /> {<br /> while (secondsDble > 0)<br /> {<br /> secondsDble -= (uint)YMDHmS.houSc;<br /> HH++;<br /> }<br /> secondsDble += (uint)YMDHmS.houSc;<br /> HH--;<br /> }<br /> if (secondsDble >= (uint)YMDHmS.minSc)//分<br /> {<br /> while (secondsDble > 0)<br /> {<br /> secondsDble -= (uint)YMDHmS.minSc;<br /> mm++;<br /> }<br /> secondsDble += (uint)YMDHmS.minSc;<br /> mm--;<br /> }<br /> if (YY != 0)<br /> returnSTR += YY.ToString() + "年";<br /> if (MM != 0)<br /> returnSTR += MM.ToString() + "月";<br /> if (DD != 0)<br /> returnSTR += DD.ToString() + "天";<br /> if (HH != 0)<br /> returnSTR += HH.ToString() + "小時";<br /> if (mm != 0)<br /> returnSTR += mm.ToString() + "分";<br /> if (secondsDble < 10)<br /> {<br /> returnSTR += "0" + TimeCount.RemainOneFigureAfterDot(secondsDble) + "秒";<br /> }<br /> else<br /> {<br /> returnSTR += TimeCount.RemainOneFigureAfterDot(secondsDble) + "秒";<br /> }<br /> return returnSTR;<br /> }</p><p> }</p><p>