指定的值不符合要求的格式 yyyy-MM-dd [英] The specified value does not conform to the required format yyyy-MM-dd

查看:44
本文介绍了指定的值不符合要求的格式 yyyy-MM-dd的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用数据注释、实体框架 Jquery 2.1.3 和 Jquery UI 1.11.4 的 .Net MVC 5 应用程序.

当我使用英国格式dd/MM/YYYY"呈现输入类型为日期的编辑表单时;使用 Google Chrome 时出现以下错误消息:

<块引用>

指定的值10/10/2001"不符合要求的格式yyyy-MM-dd".jquery-2.1.3.js:5317

模型

公共类 MyModel{[Column(TypeName = "date"), DataType(DataType.Date), Display(Name = "My date")][DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]公共字符串 MyDate { 获取;放;}}

标记

"

<input class="text-box single-line" data-val="true" data-val-date="我的日期字段必须是日期."id="MyDate" name="MyDate" type="date" value="10/10/2001"/>

输入控件中的值设置正确,但浏览器中没有显示日期.我首先认为这是 jQuery 的问题,因为它出现了 jQuery 脚本文件,但在 IE 和 Firefox 中测试时一切正常.

然后我假设这是我在 chrome 中的区域设置,因为默认情况下 Chrome 认为每个英语都在美国,我将区域设置更改为英国,但仍然出现相同的问题.

一个简单的解决方法是将我的模型中的格式更改为通用格式,但对于英国用户来说这有点陌生.

有没有办法告诉 chrome 接受dd/MM/YYYY"中的日期格式?

解决方案

HTML5 日期选择器的规范规定日期必须采用 yyyy-MM-dd(ISO 格式)格式.这意味着你 DisplayFormatAttribute 必须是

[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]公共字符串 MyDate { 获取;放;}

或者,您可以使用

手动添加格式

@Html.TextBoxFor(m => m.MyDate, "{0:yyyy-MM-dd}", new { @type = "date" })

后面的选项允许您保留 DataFormatString = "{0:dd/MM/yyyy}") 以在 @Html.DisplayFor() 中使用>

I have a .Net MVC 5 application that is using Data Annotations, Entity-Framework Jquery 2.1.3 and Jquery UI 1.11.4.

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:

The specified value '10/10/2001' does not conform to the required format, '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.

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.

Is there a way to tell chrome that accept date formats in "dd/MM/YYYY"?

解决方案

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"  })

The later option allows you to keep the DataFormatString = "{0:dd/MM/yyyy}") for usage in @Html.DisplayFor()

这篇关于指定的值不符合要求的格式 yyyy-MM-dd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆