C#秒轉換為時分秒(轉)


C#將秒數轉化為時分秒格式00:00:00
//將秒數轉化為時分秒
private string sec_to_hms(long duration)
{
TimeSpan ts = new TimeSpan(0, 0, Convert.ToInt32(duration));
string str = "";
if (ts.Hours > 0)
{
str = String.Format("{0:00}", ts.Hours)+":"+ String.Format("{0:00}", ts.Minutes) + ":" + String.Format("{0:00}", ts.Seconds);
}
if (ts.Hours == 0 && ts.Minutes > 0)
{
str = "00:"+ String.Format("{0:00}", ts.Minutes)+":" + String.Format("{0:00}", ts.Seconds);
}
if (ts.Hours == 0 && ts.Minutes == 0)
{
str = "00:00:" + String.Format("{0:00}",ts.Seconds);
}
return str;
}
結果為:

00:00:10

00:01:10

03:01:10等
https://blog.csdn.net/qq_16687863/article/details/101035803

================================================================

private string GetHMSTime(float time)
{
float h = Mathf.FloorToInt(time / 3600f);
float m = Mathf.FloorToInt(time / 60f - h * 60f);
float s = Mathf.FloorToInt(time - m * 60f - h * 3600f);
return h.ToString("00") + ":" + m.ToString("00") + ":" + s.ToString("00");
}
https://blog.csdn.net/RocketJ/article/details/113503161


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM