What formats does ASP.NET MVC expect for DateTime for model binding to work correctly?

I have a situation where I need to set the date and time in a hidden field through javascript. I have to catch this hidden field value as a parameter of my server action.

The problem is that I donโ€™t know which format to use for writing datetime in a hidden field. I googled a lot and did not find a table explaining which datetime formats are compatible with the default model binding mechanism.

I tried using iso datetime: "yyyy-mm-dd'T'HH: MM: ss" and iso utc datetime: "UTC: yyyy-mm-dd'T'HH: MM: ss'Z '" without success ".

PS: I know that ASP.NET MVC takes into account whether it is GET or POST when working with formats and cultures. My culture is PT-BR, and this is POST.

+4
source share
2 answers

Take a look at the blog post , which explains very well what happens and how the default connecting device works.

In fact, the middleware uses InvariantCulture parameters for GET and a culture-specific format for POST parameters by default. Since you are sending a POST request, the ISO format is not recognized. In this case, you can either change the culture of your application in the <globalization> element of your web.config, or write your own connecting device to override the default behavior.

+4
source

This should work for your culture: "yyyy-mm-dd HH:MM:ss"

For the record, I literally just confirmed this exact problem for en-US, and it should look like this: DD/MM/YYYY HH:MM:SS AM|PM , which in the real example will be 09/15/2011 09:15:35 AM

Another format for the 24-hour hour would be DD/MM/YYYY HH:MM:SS real-life example 09/23/2011 15:30:20

0
source

All Articles