在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
class TimeCount { // 临时变量,存放当前类能表示的最大年份值 private static ulong MaxYear = 0; /// <summary> /// 获取毫秒能表示的最大年份数 /// </summary> /// <returns>年份数最大值</returns> public static ulong GetMaxYearCount() { if (TimeCount.MaxYear != 0) return TimeCount.MaxYear; else { double ulongMax = Math.Pow(2, 64); double yearToMiliseconds = (double)12 * (double)30 * (double)24 * (double)3600 * (double)1000; TimeCount.MaxYear = (ulong)(ulongMax / yearToMiliseconds); return TimeCount.MaxYear; } } /// <summary> /// 把当前系统时间转换成毫秒 /// </summary> /// <returns>当前系统时间所对应的毫秒数</returns> public static ulong GetCurrentTimeByMiliSec() { DateTime t = DateTime.Now; ulong millisecond = ((((((ulong)t.Year * 12 + (ulong)t.Month) * 30 + (ulong)t.Day) * 24 + (ulong)t.Hour) * 60 + (ulong)t.Minute) * 60 + (ulong)t.Second) * 1000 + (ulong)(t.Millisecond); return millisecond; } /// <summary> /// 不太实用的函数,给定三个输入,得到时间差,以字符串形式返回 /// </summary> /// <param name="str">时间前面的前缀,例如“绘制XX所用时间为:”</param> /// <param name="a">第一个时间</param> /// <param name="b">第二个时间,无所谓先后,总之最后会取绝对值</param> /// <returns></returns> public static string GetTimeInterval(string str, ulong a, ulong b) { ulong interval = b - a; str += Convert.ToString(Math.Abs(( ((double)interval)/1000))); str += "秒"; return str; } /// <summary> /// 得到两个给定时间(毫秒)之间的时间差,最终值会去绝对值 /// </summary> /// <param name="a">早一些的时间(毫秒)</param> /// <param name="b">晚一些的时间(毫秒)</param> /// <returns></returns> public static double GetTimeInterValBySec(ulong a, ulong b) { return Math.Abs(((double)b - (double)a)/(double)1000); } /// <summary> /// 小数点后保留一位小数 /// </summary> /// <param name="double1">一个小数</param> /// <returns>一个字符串</returns> public static string RemainOneFigureAfterDot(double double1) { string tempStr = double1.ToString(); string double1str; if (tempStr == "") { return "0.0"; } int index = tempStr.IndexOf("."); if (index == -1) { double1str = tempStr + ".0"; return double1str; } else { if (tempStr.Length < index + 2) return tempStr + ".0"; else { double1str = tempStr.Substring(0, index + 2); return double1str; } } } /// <summary> /// 年月日时分化成秒 /// </summary> public enum YMDHmS { yearSc = 365 * 24 * 3600, monSc = 30 * 24 * 3600, daySc = 24 * 3600, houSc = 3600, minSc = 60 } /// <summary> /// 把一个String格式的以秒为单位的时间,转化成年月日时分秒为单位的时间字符串,并最后的秒保留一位小数 /// </summary> /// <param name="seconds">秒数,string格式</param> /// <returns></returns> public static string SecondsToYYMMDDhhmmss(string seconds) { double secondsDble = Convert.ToDouble(seconds); return TimeCount.SecondsToYYMMDDhhmmss(secondsDble); } /// <summary> /// 把一个int格式的以秒为单位的时间,转化成年月日时分秒为单位的时间字符串,并最后的秒保留一位小数 /// </summary> /// <param name="seconds">秒数,int格式</param> /// <returns></returns> public static string SecondsToYYMMDDhhmmss(int seconds) { double secondsDble = Convert.ToDouble(seconds); return TimeCount.SecondsToYYMMDDhhmmss(secondsDble); } /// <summary> /// 把一个ulong格式的以秒为单位的时间,转化成年月日时分秒为单位的时间字符串,并最后的秒保留一位小数 /// </summary> /// <param name="seconds">秒数,ulong格式</param> /// <returns></returns> public static string SecondsToYYMMDDhhmmss(ulong seconds) { double secondsDble = Convert.ToDouble(seconds); return TimeCount.SecondsToYYMMDDhhmmss(secondsDble); } /// <summary> /// 把一个double格式的以秒为单位的时间,转化成年月日时分秒为单位的时间字符串,并最后的秒保留一位小数 /// </summary> /// <param name="secondsDble">秒数,double格式</param> /// <returns></returns> public static string SecondsToYYMMDDhhmmss(double secondsDble) { string returnSTR = ""; if (secondsDble == 0) { return "0.0秒"; } int YY = 0, MM = 0, DD = 0, HH = 0, mm = 0; if (secondsDble >= (uint)YMDHmS.yearSc)//年 { while (secondsDble > 0) { secondsDble -= (uint)YMDHmS.yearSc; YY++; } secondsDble += (uint)YMDHmS.yearSc; YY--; } if (secondsDble >= (uint)YMDHmS.monSc)//月 { while (secondsDble > 0) { secondsDble -= (uint)YMDHmS.monSc; MM++; } secondsDble += (uint)YMDHmS.monSc; MM--; } if (secondsDble >= (uint)YMDHmS.daySc)//日 { while (secondsDble > 0) { secondsDble -= (uint)YMDHmS.daySc; DD++; } secondsDble += (uint)YMDHmS.daySc; DD--; } if (secondsDble >= (uint)YMDHmS.houSc)//时 { while (secondsDble > 0) { secondsDble -= (uint)YMDHmS.houSc; HH++; } secondsDble += (uint)YMDHmS.houSc; HH--; } if (secondsDble >= (uint)YMDHmS.minSc)//分 { while (secondsDble > 0) { secondsDble -= (uint)YMDHmS.minSc; mm++; } secondsDble += (uint)YMDHmS.minSc; mm--; } if (YY != 0) returnSTR += YY.ToString() + "年"; if (MM != 0) returnSTR += MM.ToString() + "月"; if (DD != 0) returnSTR += DD.ToString() + "天"; if (HH != 0) returnSTR += HH.ToString() + "小时"; if (mm != 0) returnSTR += mm.ToString() + "分"; if (secondsDble < 10) { returnSTR += "0" + TimeCount.RemainOneFigureAfterDot(secondsDble) + "秒"; } else { returnSTR += TimeCount.RemainOneFigureAfterDot(secondsDble) + "秒"; } return returnSTR; } } 原文链接:C# 版本的 计时器类 精确到微秒 秒后保留一位小数 支持年月日时分秒带单位的输出 |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论