ConditionRacer ConditionRacer - 2 months ago 16
C# Question

Binding TimeSpan larger than 24 hours

I have a WebApi Controller like this:

public async Task<IHttpActionResult> PutSomeValue(TimeSpan ts){ ... }


If I PUT this, it binds as 23 hours:

{
"ts": "23:00:00"
}


But if I PUT this, it binds as 24 days:

{
"ts": "24:00:00"
}


And if I PUT this, it fails with a 400:

{
"ts": "24:30:00"
}


How can I correct this?

Answer

Take a look at TimeSpan.Parse docs- they show the full format spec. Looks like 12.12:12:12 should give you 12d 12h 12m 12s.