rak*_*los 12 .net c# formatting timespan
请原谅粗略的代码,我试图以秒为单位显示视频的持续时间.我有一个下面但它没有正常工作.
我希望它能很好地显示 - 即应该显示9m:59s而不是09m:59s.
如果小时为零则不显示小时数,如果小时数为零则不显示分钟数.
public static string GetTimeSpan(int secs)
{
TimeSpan t = TimeSpan.FromSeconds(secs);
string answer;
if (secs < 60)
{
answer = string.Format("{0:D2}s", t.Seconds);
}
else if (secs < 600)//tenmins
{
answer = string.Format("{0:m}m:{1:D2}s", t.Minutes, t.Seconds);
}
else if (secs < 3600)//hour
{
answer = string.Format("{0:mm}m:{1:D2}s", t.Minutes, t.Seconds);
}
else
{
answer = string.Format("{0:h}h:{1:D2}m:{2:D2}s",
t.Hours,
t.Minutes,
t.Seconds);
}
return answer;
}
Run Code Online (Sandbox Code Playgroud)
Han*_*ing 27
就像是:
public static string PrintTimeSpan(int secs)
{
TimeSpan t = TimeSpan.FromSeconds(secs);
string answer;
if (t.TotalMinutes < 1.0)
{
answer = String.Format("{0}s", t.Seconds);
}
else if (t.TotalHours < 1.0)
{
answer = String.Format("{0}m:{1:D2}s", t.Minutes, t.Seconds);
}
else // more than 1 hour
{
answer = String.Format("{0}h:{1:D2}m:{2:D2}s", (int)t.TotalHours, t.Minutes, t.Seconds);
}
return answer;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10131 次 |
| 最近记录: |