编程语言
首页 > 编程语言> > C# winform 将秒转化成时分秒

C# winform 将秒转化成时分秒

作者:互联网

直接复制即可

 

#region 将秒转化成时分秒
/// <summary>
/// 将int秒数转化成string时分秒
/// </summary>
/// <param name="sec"></param>
/// <returns></returns>
private string SecToMin(Int64 sec)
{
try
{
Int64 hour;
Int64 Mintue;
Int64 second;
if (sec < 0) { sec = 0; }
second = sec % 60;
sec = sec - second;
sec = sec / 60;
Mintue = sec % 60;
sec = sec - Mintue;
hour = sec / 60;
return string.Format("{0:000}时{1:00}分{2:00}秒", hour, Mintue, second);
}
catch
{
WLog.Write("将秒转化成时分秒" + " " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), "\r\n\r\n");
return "12.24分";
}
}
#endregion

 

 

 

同理 也可以将时分秒转化成分钟等等

 

#region 将时分秒转化成分钟
/// <summary>
/// 将int秒数转化成string分
/// </summary>
/// <param name="sec"></param>
/// <returns></returns>
private string TimeToMin(Int64 sec)
{
Int64 hour;
Int64 Mintue;
Int64 second;
if (sec < 0) { sec = 0; }
second = sec % 60;
sec = sec - second;
sec = sec / 60;
Mintue = sec % 60;
sec = sec - Mintue;
hour = sec / 60;
Int64 a = 0;
if (second > 30) { a = 1; }
string result = (hour * 60 + Mintue + a).ToString() + "分";
if (hour == 0 && Mintue == 0) { result = second.ToString() + "秒"; }
return result;
}
#endregion

标签:Mintue,hour,C#,60,second,sec,Int64,时分秒,winform
来源: https://www.cnblogs.com/Lee597/p/15832747.html