dattebayo
dattebayo

Reputation: 2072

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 formatting string to be "mm:ss", I should get the elapsed time as 66:40.

Thanks Datte

Upvotes: 2

Views: 3695

Answers (2)

Polynomial
Polynomial

Reputation: 28316

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.

Upvotes: 0

porges
porges

Reputation: 30580

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.)

Upvotes: 3

Related Questions