Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TimeSpan and "24:00" parse error in Asp.net MVC

I have a modal dialog in my web application where users are able to enter a time range between 00:00 and 24:00. A range slider is used to select this range.

Everything works as expected except that whenever user sets the right range handle to have a value of 24:00 default model binder can't parse this TimeSpan.

public class Timing
{
    public TimeSpan Starts { get; set; }
    public TimeSpan Ends { get; set; }
}

My object that gets sent back to server has an IList<Timing> property.

So. The problem is just that string value "24:00" can't be parsed to TimeSpan instance. Is it possible to convince default model binder to recognise such string value?

I would like to avoid changing 24:00 on the client to 00:00. I know that I have Starts and Ends properties but my model validation validates that Ends is always greater than Starts. Manual changing to 23:59 is also cumbersome. Basically is it possible to pass 24:00 and still get parsed on the server.

like image 756
Robert Koritnik Avatar asked Dec 28 '22 18:12

Robert Koritnik


1 Answers

I think the range is fractionally too large. 24:00 is in fact 00:00 the next day. so they should go from 00:00.00 to 23:59.99 or whatever.

FINAL ANSWER(?) Change 24:00 on the client to 1.0:00. This will work because TimeSpan.Parse("1.0:00").TotalHours returns 24

EDIT: See the documentation here: http://msdn.microsoft.com/en-us/library/se73z7b9.aspx. It shows the maximum range for days, hours, mins, etc. For hours it's 0 to 23 as per my comment below.

EDIT: If you are just letting them choose an integer for hours, then parse it on the server.

eg. TimeSpan ts = TimeSpan.FromHours(24) returns 1.00:00:00 And of course you can always say ts.TotalHours and it returns 24.

like image 149
Tom Chantler Avatar answered Jan 16 '23 01:01

Tom Chantler