views:

375

answers:

2

Hi

I have a double value in seconds and i would like to use a Numeric Format String to display it as mm:ss or hh:mm:ss. Is this possible? Havent found anything about it on MSDN?

The reason is that we use a Telerik Chart that displays our data and since I cant change the uderlying format I have to hook the format string into their chart api.

Thanks in advance Johan

+1  A: 

You're looking for the TimeSpan class.

Carra
A: 

Use the following:

var ts = new TimeSpan(0, 0, 0, (int)doubleValueOfSeconds, 0);

Of course you might want to consider more accurate rounding of the double value, rather than truncation as in my example.

Take a look at TimeSpan on MSDN to get the string value, though the simplest current culture-specific thing would be TimeSpan.ToString().

UPDATE to updated question:

You can't directly format a number of seconds to a date and time. Formatting is essentially string manipulation, and you need it to do some maths before that formatting happens.

Use my example above, then call TimeSpan.ToString(formatString); to get the string values to give to the control.

That's assuming you haven't simply missed something. I personally think the Telerik controls are waaaay to complex, but I'm sure they'll have formatting built-in. Essentially, write code using my example above to create the data with timespans, then use the inbuilt format string to tell the control how to format the timespan for the given data series.

Neil Barnwell
Actually i cant (as far as I know) hook in any code, the chart control takes a numeric format string that formats the value in the chart.So I cant use any classes to calculate the value, thats not the problem, the problem is how can i do it with a numeric format string (if its even possible?)But still thanks for your answers
JWendel
I've updated my answer, hope it now helps. You really need to create the data up front as timespan objects, *before* you give the data to the chart. There's no way to format a number to a hours/mins value. You have to build the hours/mins value using my example and format that object accordingly.
Neil Barnwell