jQuery 指定的值不符合要求的格式 yyyy-MM-dd
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30798906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The specified value does not conform to the required format yyyy-MM-dd
提问by Andy Clark
I have a .Net MVC 5 application that is using Data Annotations, Entity-Framework Jquery 2.1.3 and Jquery UI 1.11.4.
我有一个使用数据注释、实体框架 Jquery 2.1.3 和 Jquery UI 1.11.4 的 .Net MVC 5 应用程序。
When I render an edit form with an input of type date using the UK format "dd/MM/YYYY"; the following error message appears when using Google Chrome:
当我使用英国格式“dd/MM/YYYY”呈现输入类型为日期的编辑表单时;使用 Google Chrome 时出现以下错误消息:
The specified value '10/10/2001' does not conform to the required format, 'yyyy-MM-dd'. jquery-2.1.3.js:5317
指定的值“10/10/2001”不符合要求的格式“yyyy-MM-dd”。jquery-2.1.3.js:5317
Model
模型
public class MyModel
{
[Column(TypeName = "date"), DataType(DataType.Date), Display(Name = "My date")]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
public string MyDate { get; set; }
}
Mark up
标记
<input class="text-box single-line" data-val="true" data-val-date="The field My date must be a date." id="MyDate" name="MyDate" type="date" value="10/10/2001" />
The value is set correctly in the input control but the date does not appear in the browser. I first thought this was an issue with jQuery as it is appearing the jQuery script file, but when testing in IE and Firefox everything is working fine.
该值在输入控件中设置正确,但日期未出现在浏览器中。我首先认为这是 jQuery 的问题,因为它出现了 jQuery 脚本文件,但是在 IE 和 Firefox 中测试时一切正常。
I then assumed it was my regional setting in chrome as by default Chrome thinks everyone English is in America, I changed the regional setting to UK and still the same issue appears.
然后我假设这是我在 chrome 中的区域设置,因为默认情况下 Chrome 认为每个英语都在美国,我将区域设置更改为英国,但仍然出现相同的问题。
A simple fix would be to change the format in my model to universal but to UK users this is a little alien.
一个简单的解决方法是将我的模型中的格式更改为通用格式,但对于英国用户来说,这有点陌生。
Is there a way to tell chrome that accept date formats in "dd/MM/YYYY"?
有没有办法告诉chrome接受“dd/MM/YYYY”中的日期格式?
回答by
The specifications for the HTML5 date picker state that the date must be in the format yyyy-MM-dd
(ISO format). This means that you DisplayFormatAttribute
must be
HTML5 日期选择器的规范规定日期必须采用格式yyyy-MM-dd
(ISO 格式)。这意味着你DisplayFormatAttribute
必须
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public string MyDate { get; set; }
Alternatively you can manually add the format using
或者,您可以使用手动添加格式
@Html.TextBoxFor(m => m.MyDate, "{0:yyyy-MM-dd}", new { @type = "date" })
The later option allows you to keep the DataFormatString = "{0:dd/MM/yyyy}")
for usage in @Html.DisplayFor()
后面的选项允许您将DataFormatString = "{0:dd/MM/yyyy}")
for 的用法保留在@Html.DisplayFor()
回答by Petre Ionescu
The issue could be from the type="date". That was my situation anyway. Worked once it was changed to type="text". If the interface is built with an MVC wrapper, you need to replace or set the html attribute accordingly.
问题可能来自 type="date"。反正我的情况就是这样。将其更改为 type="text" 后即可使用。如果接口是用 MVC 包装器构建的,则需要相应地替换或设置 html 属性。
回答by M. Ruiz
You can use the InputTagHelper.Format
您可以使用 InputTagHelper.Format
<input asp-for="MyDate" asp-format="{0:yyyy-MM-dd}" />
回答by devlin carnate
Set the type
attribute to text.
将type
属性设置为文本。
@Html.TextBoxFor(m => m.MyDate, new { @type = "text" })
回答by user3357031
I'd like to highlight something in the answer here.
我想在这里强调一些答案。
If you are building an application that has Globalization and Localization then the method of passing in a format using a Html helper will be better. You can also just use EditorFor and pass in the necessary id or class for datepicker.
如果您正在构建具有全球化和本地化功能的应用程序,那么使用 Html 助手传递格式的方法会更好。您也可以只使用 EditorFor 并为日期选择器传递必要的 id 或类。