TheSun
TheSun

Reputation: 11

How to convert Timespan as a value without days in ASP.NET Core?

I have a timespan data type, and when calculating it, if the value exceeds a day, it will have the format: d.hh:mm:ss and I expect it to have the format hh:mm:ss.

Example: 1.6:00:00 -> 30:00:00

Can I get your help with that?

Upvotes: 0

Views: 57

Answers (1)

Mark Seemann
Mark Seemann

Reputation: 233125

I have a timespan data type, and when calculating it, if the value exceeds a day, it will have the format: d.hh:mm:ss and I expect it to have the format hh:mm:ss.

The TimeSpan data structure doesn't 'have a format'. Behind the scenes, IIRC it's just an integer that measures a number of ticks, a tick being 100 ns.

When you call e.g. ToString on it, you get a string representation of the value, and this default string representation does, indeed, have the format [-][d.]hh:mm:ss[.fffffff].

As the various comments have already pointed out, you're probably best off writing a custom formatting function for your particular purpose.

On the other hand, if you just need the total number of hours, there's no reason to turn the value into a string. In that case, you can just get the value of the TotalHours property.

Upvotes: 1

Related Questions