'Google Chrome is showing the date format yyyy-MM-dd instead of dd/MM/yyyy
The specified value "18/08/2017"
does not conform to the required format, yyyy-MM-dd
Google Chrome doesn't load date in dd/MM/yyyy
format from the controller method. I want to load date into date type field. google chrome gives me a warning. How do I fix this
The specified value "18/08/2017"
does not conform to the required format, yyyy-MM-dd
.
here is my code in the view
@Html.EditorFor(model => model.DateReceived, "{0:dd/MM/yyyy}",
new {htmlAttributes = new { @class = "form-control", @type="date" } })
Solution 1:[1]
Chrome wants "yyyy-MM-dd" -> 2017/08/18
You gave chrome "dd-MM-yyyy" -> 18/08/2017
You need to give Chrome the format it wants.
Change to:
@Html.EditorFor(model => model.DateReceived, "{0:yyyy/MM/dd}", new {htmlAttributes = new { @class = "form-control", @type="date" } })
Solution 2:[2]
Please write the below javascript in your page
<script type="text/jscript">
$(function () {
$.validator.methods.date = function (value, element) {
if ($.browser.webkit) {
//ES - Chrome does not use the locale when new Date objects instantiated:
var d = new Date();
return this.optional(element) || !/Invalid|NaN/.test(new Date(d.toLocaleDateString(value)));
}
else {
return this.optional(element) || !/Invalid|NaN/.test(new Date(value));
}
};
});
and in view write this
@Html.EditorFor(model => model.DateReceived, "{0:dd/MM/yyyy}",
new { @class = "form-control" })
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | C. McCoy IV |
Solution 2 | Nripendra Ojha |