指定的值不符合要求的格式YYYY-MM-DD [英] The specified value does not conform to the required format yyyy-MM-dd
问题描述
我有一个使用数据注释,实体框架jQuery的2.1.3和jQuery UI 1.11.4一个.net MVC 5应用程序。
I have a .Net MVC 5 application that is using Data Annotations, Entity-Framework Jquery 2.1.3 and Jquery UI 1.11.4.
当我渲染用英国的格式为DD / MM / YYYY式日期的输入编辑的形式;使用谷歌Chrome浏览器时,会出现以下错误信息:
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:
指定的值'10 / 10/2001'不符合要求的格式,'YYYY-MM-DD'。 jQuery的-2.1.3.js:5317
The specified value '10/10/2001' does not conform to the required format, 'yyyy-MM-dd'. jquery-2.1.3.js:5317
型号
public class MyModel
{
[Column(TypeName = "date"), DataType(DataType.Date), Display(Name = "My date")]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
public string MyDate { get; set; }
}
标记了
<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" />
的值正确设定在输入控制但日期没有出现在浏览器中。我首先想到这是与jQuery的一个问题,因为它是出现在jQuery的脚本文件,但在IE测试和Firefox一切工作正常时。
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.
然后,我以为这是在我的铬区域设置为默认浏览器认为每个人的英语是在美国,我改变了区域设置以英国和仍然出现同样的问题。
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.
有一个简单的解决将是在我的模型的格式更改为通用,但英国用户来说,这是一个小外星人。
A simple fix would be to change the format in my model to universal but to UK users this is a little alien.
有没有办法告诉铬接受日期格式DD / MM / YYYY?
Is there a way to tell chrome that accept date formats in "dd/MM/YYYY"?
推荐答案
的规格为HTML5日期选取器状态的日期必须是在格式 YYYY-MM-DD
(ISO格式)。这意味着你 DisplayFormatAttribute
必须
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
[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" })
后来的选项可以让你保持 DataFormatString ={0:DD / MM / YYYY})
在 @Html使用。 DisplayFor()
这篇关于指定的值不符合要求的格式YYYY-MM-DD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!