Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML-5 date field shows as "mm/dd/yyyy" in Chrome, even when valid date is set

I just upgraded an ASP.Net MVC application to MVC-4. The field editor for inputs based on DateTime values now include's the HTML-5 type="date" attribute/value declaration.

Now, when viewing in Chrome, my date inputs show up with "mm/dd/yyyy" in the input field:

date field with 'mm/dd/yyyy' in box

Even when I pass in a correctly-formatted date with the value attribute:

<input value="2012/10/02" type="date"/> 

I'm still getting "mm/dd/yyyy" in the input box, which is there until the user manually changes the value.

The problem appears to be with Chrome, and is independent of my back-end framework. See this problem in action: jsFiddle

...big problem for editing records, of course. If the user pulls up a record that already has a valid date, it won't pass validation on submit, unless s/he clicked into the field and reset the value manually.

No problems with the other browsers.

Is this a Chrome bug? Or have I missed something about the way the HTML-5 date field is supposed to work?

UPDATE
See this revise fiddle: http://jsfiddle.net/HudMe/5/ It has both an HTML-4 and an HTML-5 date input, each with "10/01/2012" set as the value on page-load.

Click into either date field. The Javascript should pup up an alert with the value of the field for that element.

Because a valid date has been passed in with the value attribute, this should show "10/01/2012", but in Chrome, for the HTML-5 date field, nothing shows. Reset this value by hand, then click again, and it will now show.

The value from the HTML5 field shows and alerts as expected without adjustment after pageload in Safari, Firefox, IE and Opera.

Note on accepted answer:
For other users of Asp.net mvc-4, you can adjust the display format with the [DisplayFormat] attribute on the DateTime field declaration in your view-model. (found at https://stackoverflow.com/a/12634470/613004 )

like image 719
Faust Avatar asked Feb 01 '13 13:02

Faust


People also ask

How do I fix the date format in HTML?

4-on the second function you can use any format you want instead of day+" / "+month+" / "+year for example year+" / "+month+" / "+day and in the text input use a placeholder or value as yyyy / mm / dd for the user when the page load.

How do I change the date format from YYYY MM DD in HTML?

To set and get the input type date in dd-mm-yyyy format we will use <input> type attribute. The <input> type attribute is used to define a date picker or control field. In this attribute, you can set the range from which day-month-year to which day-month-year date can be selected from.

Is date valid in html5 forms?

The date and time input types produce controls for entering dates and time in the correct format, including providing native date pickers in some browsers. Date related types include: type=date (yyyy-mm-dd) Year, month, and day, with no time.

How do I format a date field in HTML?

dd-mm-yyyy. mm-dd-yyyy.


2 Answers

In chrome to set the value you need to do YYYY-MM-DD i guess because this worked : http://jsfiddle.net/HudMe/6/

So to make it work you need to set the date as 2012-10-01

like image 85
eric.itzhak Avatar answered Sep 22 '22 16:09

eric.itzhak


Had the same problem. A colleague solved this with jQuery.Globalize.

<script src="/Scripts/jquery.validate.js" type="text/javascript"></script> <script src="/Scripts/jquery.globalize/globalize.js" type="text/javascript"></script> <script src="/Scripts/jquery.globalize/cultures/globalize.culture.nl.js"></script> <script type="text/javascript">     var lang = 'nl';      $(function () {         Globalize.culture(lang);     });      // fixing a weird validation issue with dates (nl date notation) and Google Chrome     $.validator.methods.date = function(value, element) {         var d = Globalize.parseDate(value);         return this.optional(element) || !/Invalid|NaN/.test(d);     }; </script> 

I am using jQuery Datepicker for selecting the date.

like image 35
Wim Avatar answered Sep 19 '22 16:09

Wim