Need only HH:MM:SS in time span string

I have a time span string as

1.21:00:00

it means 45 hours and i need it as

45:00:00

is it possible to do that in c#?

Jon Skeet
people
quotationmark

Unfortunately I don't think the TimeSpan custom formatting makes this feasible :(

You could either perform the string formatting yourself...

string text = (int) span.TotalHours + span.ToString(@"\:mm\:ss");

Or

string text = string.Format(@"{0}:{1:mm\:ss}", (int) span.TotalHours, span);

... or you could use my Noda Time library, which does allow for this:

// Or convert from a TimeSpan to a Duration
var duration = Duration.FromHours(50);
var durationPattern = DurationPattern.CreateWithInvariantCulture("HH:mm:ss");
Console.WriteLine(durationPattern.Format(duration)); // 50:00:00

Obviously I'd recommend moving your whole code base over to Noda Time to make all your date/time code clearer, but I'm biased :)

people

See more on this question at Stackoverflow