justdmitry / BootstrapMvc

Write less bootstrap markup
http://bootstrapmvc.net
MIT License
14 stars 10 forks source link

Html5 DateTime format string #4

Closed kbalint closed 7 years ago

kbalint commented 7 years ago

Hi. consider the following:

BootstrapMvc.Controls.Input.DateInputModeDefault = DateInputMode.Html5; @Bootstrap.FormGroupFor(m => m.DateValue).Control(Bootstrap.Input(InputType.DatetimeLocal))

it generates the following output in Html:

<input class="form-control" id="DateValue" name="DateValue" type="datetime-local" value="2017-06-02T17:31:00.0000000">

The value is not well formatted. Instead of.ToString("o"), it has to be .ToString('YYYY-MM-DD HH:mm') explicitly. (Or even better, a custom DateTimeFormatString property).

I've tried to change it in \BootstrapMvc-master\src\BootstrapMvc.BootstrapCommon\Components\Form_Controls\Input.cs but I can't compile the project in Visual Studio 2017, it seems like some circular reference regarding the GridSize struct. I've tried to set NET451;BOOTSTRAP3, to no avail. Anyway at least VS2017 did the automatic xproj -> csproj conversion.

Any thoughts on how to upgrade and compile the project for .NET 4.6, MVC5?

justdmitry commented 7 years ago

Hi,

RFC says that value "must have a value that is a valid floating date and time string", and "valid floating date and time" may contain either T or space as date-time delimiter. So the only problem with current format string are second fractions in time section where one, two or three digits are allowed (not seven).

I had no any "good" browser support at time of writing this lib to validate current format string. Currently, any decisions should be re-validated with all popular browsers before implementation.

kbalint commented 7 years ago

Hi, yes, I meant .ToString('YYYY-MM-DDTHH:mm') , the problem is indeed with seconds and miliseconds. As with Chrome, the warning is the following:

The specified value "2017-06-06T16:14:00.0000000" does not conform to the required format. The format is "yyyy-MM-ddThh:mm" followed by optional ":ss" or ":ss.SSS".

the bad thing is, that it fails to render anything - it is sadly according to the documentation.

kbalint commented 7 years ago

A possible solution with the DateFormatString property: https://gist.github.com/kbalint/32c69b545d01298030ebb4fb8d959346

justdmitry commented 7 years ago

I'm not sure custom format string for RFC-restricted value is really needed. RFC says that value must meet specified format string, and browser should accept it. I changed format string for datetime-local and now this input field is accepted by Chrome (and hides 00 seconds automatically). Firefox still does not understand this type and renders it as text.

Instead, I introduced format string for InputType.Date and InputType.DatetimeLocal in DateInputMode.Text (default to "d" and "G").

Also, InputType.Datetime is marked as obsolete, as soon as I can't see it in RFC anymore.

Also, InputTime() method added (to create input with InputType.Time) with fixed format HH:mm:ss.

And project is converted to VS2017 and csproj. BootstrapCommon should not be referenced to Bootstrap3 and Bootstrap4 - they must include it's source files during compilation, without referencing project/library itself. But "problems" with GridSize (and some other) are resolved to ensure seamless build.