开发者

ElapsedTime formatting in .NET

Is there a way to convert a given value to elapsed time with a formatting string in .NET? For example, if I have value 4000 and formatti开发者_运维知识库ng string to be "mm:ss", I should get the elapsed time as 66:40.

Thanks Datte


You can use the TimeSpan class to make this easier:

var elapsedTime = TimeSpan.FromSeconds(4000);
var formatted = string.Format("{0}:{1}", (int)elapsedTime.TotalMinutes, elapsedTime.Seconds);

Console.WriteLine(formatted);

(You can't use normal format strings for this since you want the total minutes instead of days/hours/minutes/etc.)


Using standard format strings there would be no way to tell which of your format strings are totals and which are component values. For example, should mm produce the total number of minutes, or the minute component? And if it's total, how do we know what the total is? It should be 66.666666 minutes, not 66.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜