How to format a TimeSpan for hours not days
Asked Answered
H

3

13

The following code

Console.WriteLine("{0:%h} hours {0:%m} minutes", 
                   new TimeSpan(TimeSpan.TicksPerDay));

produces this output:

0 hours 0 minutes

What I would like is this output:

24 hours 0 minutes

What am I missing in this format string?

P.S. I know that I could manually bust up the TimeSpan into days and hours, and multiply the two but would rather use a custom format string, as these timespans are being displayed in a silverlight datagrid and people are expecting to see horus, not days.

Honan answered 18/4, 2011 at 14:0 Comment(0)
P
15

According to MSDN, using %h will show you

The number of whole hours in the time interval that are not counted as part of days.

I think you will need to use the TotalHours property of the TimeSpan class like:

TimeSpan day= new TimeSpan(TimeSpan.TicksPerDay);
Console.WriteLine("{0} hours {1} minutes", (int)day.TotalHours, day.Minutes);

Update

If you absolutely need to be able to achieve the stated format by passing custom formatters to the ToString method, you will probably need to create your own CustomTimeSpan class. Unfortunately, you cannot inherit from a struct, so you will have to build it from the ground up.

Psalm answered 18/4, 2011 at 14:6 Comment(2)
Thanks for the update --- that's what's pushed me over the edge towards a custom type.Honan
you might have to truncate it otherwise totalhours property won't show the right figure. Dim x As New TimeSpan(0, 0, 3600) String.Format("{0}hr:{1}min", CInt(Math.Truncate(x.TotalHours)), x.Minutes)Monotony
A
10

There doesn't seem to be a format option for getting the total hours out of a TimeSpan. Your best bet would be to use the TotalHours property instead:

var mySpan = new TimeSpan(TimeSpan.TicksPerDay);
Console.WriteLine("{0} hours {1} minutes", (int)mySpan.TotalHours, mySpan.Minutes);

TotalHours returns a double as it includes the fractional hours so you need to truncate it to just the integer part.

Aftermost answered 18/4, 2011 at 14:5 Comment(4)
Right, as I said I know I can bust up the timespan, but what I don't think I can do with this approach is format the timespan with a formatstring passed into it's ToString Method.Honan
@Ralph - yes you are "busting up" the TimeSpan but there's no multiplication needed.Aftermost
@Aftermost -- true enough, TotalHours is easier than Days*24+Hours, but the difficulty remains. I need a format string. --- and I'm guessing that it's not possible.Honan
you might have to truncate it otherwise totalhours property won't show the right figure. Dim x As New TimeSpan(0, 0, 3600) String.Format("{0}hr:{1}min", CInt(Math.Truncate(x.TotalHours)), x.Minutes)Monotony
L
-1

Another possibility is:

TimeSpan day = new TimeSpan(2,1,20,0); // 2.01:20:00
Console.WriteLine("{0} hours {1} minutes", (int)day.TotalHours, day.Minutes);

Console will show:

49 hours 20 minutes

Lastex answered 5/2, 2021 at 15:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.