c#时分秒毫秒微妙_C# 版本的 计时器类:精确到微秒 秒后保留一位小数 支持年月日时分秒带单位的输出...
classTimeCount
{//临时变量,存放当前类能表示的最大年份值
private static ulong MaxYear = 0;///
///获取毫秒能表示的最大年份数///
/// 年份数最大值
public static ulongGetMaxYearCount()
{if (TimeCount.MaxYear != 0)returnTimeCount.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);returnTimeCount.MaxYear;
}
}///
///把当前系统时间转换成毫秒///
/// 当前系统时间所对应的毫秒数
public static ulongGetCurrentTimeByMiliSec()
{
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);returnmillisecond;
}///
///不太实用的函数,给定三个输入,得到时间差,以字符串形式返回///
/// 时间前面的前缀,例如“绘制XX所用时间为:”
/// 第一个时间
/// 第二个时间,无所谓先后,总之最后会取绝对值
///
public static string GetTimeInterval(string str, ulong a, ulongb)
{ulong interval = b -a;
str+= Convert.ToString(Math.Abs(( ((double)interval)/1000)));
str+= "秒";returnstr;
}///
///得到两个给定时间(毫秒)之间的时间差,最终值会去绝对值///
/// 早一些的时间(毫秒)
/// 晚一些的时间(毫秒)
///
public static double GetTimeInterValBySec(ulong a, ulongb)
{return Math.Abs(((double)b - (double)a)/(double)1000);
}///
///小数点后保留一位小数///
/// 一个小数
/// 一个字符串
public static string RemainOneFigureAfterDot(doubledouble1)
{string tempStr =double1.ToString();stringdouble1str;if (tempStr == "")
{return "0.0";
}int index = tempStr.IndexOf(".");if (index == -1)
{
double1str= tempStr + ".0";returndouble1str;
}else{if (tempStr.Length < index + 2)return tempStr + ".0";else{
double1str= tempStr.Substring(0, index + 2);returndouble1str;
}
}
}///
///年月日时分化成秒///
public enumYMDHmS
{
yearSc= 365 * 24 * 3600,
monSc= 30 * 24 * 3600,
daySc= 24 * 3600,
houSc= 3600,
minSc= 60}///
///把一个String格式的以秒为单位的时间,转化成年月日时分秒为单位的时间字符串,并最后的秒保留一位小数///
/// 秒数,string格式
///
public static string SecondsToYYMMDDhhmmss(stringseconds)
{double secondsDble =Convert.ToDouble(seconds);returnTimeCount.SecondsToYYMMDDhhmmss(secondsDble);
}///
///把一个int格式的以秒为单位的时间,转化成年月日时分秒为单位的时间字符串,并最后的秒保留一位小数///
/// 秒数,int格式
///
public static string SecondsToYYMMDDhhmmss(intseconds)
{double secondsDble =Convert.ToDouble(seconds);returnTimeCount.SecondsToYYMMDDhhmmss(secondsDble);
}///
///把一个ulong格式的以秒为单位的时间,转化成年月日时分秒为单位的时间字符串,并最后的秒保留一位小数///
/// 秒数,ulong格式
///
public static string SecondsToYYMMDDhhmmss(ulongseconds)
{double secondsDble =Convert.ToDouble(seconds);returnTimeCount.SecondsToYYMMDDhhmmss(secondsDble);
}///
///把一个double格式的以秒为单位的时间,转化成年月日时分秒为单位的时间字符串,并最后的秒保留一位小数///
/// 秒数,double格式
///
public static string SecondsToYYMMDDhhmmss(doublesecondsDble)
{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) + "秒";
}returnreturnSTR;
}
}
更多推荐


所有评论(0)