[英]The value xxx is not valid for yyy. Wrong datetime format
Hello I'm getting my dateTime from a input box with datepicker initialized with this code: 您好,我从一个带有以下代码初始化的datepicker的输入框中获取dateTime:
$(document).ready(function () {
$("#CreationDate").datepicker({ autoclose: true, todayHighlight: true, language: "pl", format: 'dd/mm/yyyy' });
})
As You can see format is dd/mm/yyyy
This is my model: 如您所见,格式为dd/mm/yyyy
这是我的模型:
public int Id { get; set; }
public int DeviceId { get; set; }
public string SerialNo { get; set; }
public System.DateTime CreationDate { get; set; }
And when I try to input my model into database controller refuses because Model.isValid=false
当我尝试将模型输入数据库控制器时,因为Model.isValid=false
If I change format to mm/dd/yyyy
model is valid but this is no option because users don't want to enter data in Us format. 如果我将格式更改为mm/dd/yyyy
模型是有效的,但这不是选项,因为用户不想以Us格式输入数据。 I put in web.config globalization line: 我输入了web.config全球化行:
<globalization culture="en-GB"/>
with no effect how should I modify my code? 没有效果,我该如何修改代码? or the only simpleen call controler action? 还是唯一简化呼叫控制程序的操作?
Problem : Your custom dateformat dd/mm/yyyy
is invalid. 问题:您的自定义日期格式dd/mm/yyyy
无效。
small mm
is used to represent Minutes
not Month
. 小mm
用于表示Minutes
而非Month
。
Solution : You should use capital MM
to represent Month
. 解决方案:您应该使用大写MM
代表Month
。 so your Format should be dd/MM/yyyy
因此您的格式应为dd/MM/yyyy
mm The minute, from 00
through 59
. mm分钟,从00
到59
。
MM The month, from 01
through 12
. MM本月,从01
到12
。
Try This: 尝试这个:
$(document).ready(function () {
$("#CreationDate").datepicker({ autoclose: true,
todayHighlight: true, language: "pl", format: 'dd/MM/yyyy' });
})
This blog post should answer your question; 这篇博文应该回答您的问题; http://weblogs.asp.net/melvynharbour/archive/2008/11/21/mvc-modelbinder-and-localization.aspx http://weblogs.asp.net/melvynharbour/archive/2008/11/21/mvc-modelbinder-and-localization.aspx
Your datepicker format should be dd/mm/yy
instead of dd/mm/yyyy
. 日期选择器格式应为dd/mm/yy
而不是dd/mm/yyyy
。 In my case the following code works greate: 就我而言,以下代码非常有效:
$(function () {
$(".datePicker").datepicker({
changeMonth: true,
changeYear: true,
dateFormat: 'dd/mm/yy',
yearRange: "2013:2014"
});
});
Please note another thing, you may change CreationDate field from System.DateTime to String. 请注意另一件事,您可以将CreationDate字段从System.DateTime更改为String。
Then, use the following code 然后,使用以下代码
DateTime CreationDate ;
DateTime.TryParseExact(yourModel.CreationDate, "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out CreationDate );
Hope, this will help. 希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.