class TimeCount
{
// 临时变量,存放当前类能表示的最大年份值
private static ulong MaxYear = ;
/// <summary>
/// 获取毫秒能表示的最大年份数
/// </summary>
/// <returns>年份数最大值</returns>
public static ulong GetMaxYearCount()
{
if (TimeCount.MaxYear != )
return TimeCount.MaxYear;
else
{
double ulongMax = Math.Pow(, );
double yearToMiliseconds = (double) * (double) * (double) * (double) * (double);
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 * + (ulong)t.Month) * + (ulong)t.Day) * + (ulong)t.Hour) * + (ulong)t.Minute) *
+ (ulong)t.Second) * + (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)/)));
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));
}
/// <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 == -)
{
double1str = tempStr + ".0";
return double1str;
}
else
{
if (tempStr.Length < index + )
return tempStr + ".0";
else
{
double1str = tempStr.Substring(, index + );
return double1str;
}
}
}
/// <summary>
/// 年月日时分化成秒
/// </summary>
public enum YMDHmS
{
yearSc = * * ,
monSc = * * ,
daySc = * ,
houSc = ,
minSc =
}
/// <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 == )
{
return "0.0秒";
}
int YY = , MM = , DD = , HH = , mm = ;
if (secondsDble >= (uint)YMDHmS.yearSc)//年
{
while (secondsDble > )
{
secondsDble -= (uint)YMDHmS.yearSc;
YY++;
}
secondsDble += (uint)YMDHmS.yearSc;
YY--;
}
if (secondsDble >= (uint)YMDHmS.monSc)//月
{
while (secondsDble > )
{
secondsDble -= (uint)YMDHmS.monSc;
MM++;
}
secondsDble += (uint)YMDHmS.monSc;
MM--;
}
if (secondsDble >= (uint)YMDHmS.daySc)//日
{
while (secondsDble > )
{
secondsDble -= (uint)YMDHmS.daySc;
DD++;
}
secondsDble += (uint)YMDHmS.daySc;
DD--;
}
if (secondsDble >= (uint)YMDHmS.houSc)//时
{
while (secondsDble > )
{
secondsDble -= (uint)YMDHmS.houSc;
HH++;
}
secondsDble += (uint)YMDHmS.houSc;
HH--;
}
if (secondsDble >= (uint)YMDHmS.minSc)//分
{
while (secondsDble > )
{
secondsDble -= (uint)YMDHmS.minSc;
mm++;
}
secondsDble += (uint)YMDHmS.minSc;
mm--;
}
if (YY != )
returnSTR += YY.ToString() + "年";
if (MM != )
returnSTR += MM.ToString() + "月";
if (DD != )
returnSTR += DD.ToString() + "天";
if (HH != )
returnSTR += HH.ToString() + "小时";
if (mm != )
returnSTR += mm.ToString() + "分";
if (secondsDble < )
{
returnSTR += "" + TimeCount.RemainOneFigureAfterDot(secondsDble) + "秒";
}
else
{
returnSTR += TimeCount.RemainOneFigureAfterDot(secondsDble) + "秒";
}
return returnSTR;
}
}
原文链接:C# 版本的 计时器类 精确到微秒 秒后保留一位小数 支持年月日时分秒带单位的输出