javascript 值“XX/YY/ZZZZ”对 DateTime 变量无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22757560/
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 value 'XX/YY/ZZZZ" is not valid for DateTime variable
提问by szpic
On my webpage I have input where I keep current date:
在我的网页上,我输入了保持当前日期的位置:
@Html.TextBoxFor(model => model.CreationDate, new { @class = "form-control datepicker", @Value = DateTime.Now.ToString("dd'/'M'/'yyyy"), type = "datetime" })
which renders into:
呈现为:
<input value="31/3/2014" class="form-control datepicker" data-val="true" data-val-date="The field CreationDate must be a date." data-val-required="Data jest wymagana" id="CreationDate" name="CreationDate" type="datetime">
As You can see the default data is 31/3/2014
如您所见,默认数据是 31/3/2014
But when I'm trying to send it to my method using Ajax I got error from ModelState
:
但是,当我尝试使用 Ajax 将其发送到我的方法时,出现以下错误ModelState
:
The value "31/3/2014" is not valid for CreationDate.
and this is what is in model:
这就是模型中的内容:
+ CreationDate {0001-01-01 00:00:00} System.DateTime
To this input box is also set bootstrap datepicker
:
还设置了此输入框bootstrap datepicker
:
$('.datepicker').datepicker({
format: "dd/MM/yyyy",
language: "pl",
autoclose: true,
todayHighlight: true
});
which returns data like:
它返回如下数据:
"31/Marzec/2014"
and with data above the ModelState.Valid=true
并且数据高于 ModelState.Valid=true
but there is need that input box default data should be valid. User should use Datepicker only when he needs to change to data other that current date.
但是需要输入框默认数据应该是有效的。用户仅在需要更改为当前日期以外的数据时才应使用 Datepicker。
How should I modify my code? I tryied messing with Formats in toString()
with no effects.
我应该如何修改我的代码?我尝试在toString()
没有任何效果的情况下弄乱格式。
Here my Model:
这是我的模型:
public class CreateDeviceInstance
{
public int Id { get; set; }
public int DeviceId { get; set; }
public string SerialNo { get; set; }
[Required(ErrorMessage="Data jest wymagana")]
public System.DateTime CreationDate { get; set; }
public Nullable<int> ProjectId { get; set; }
public bool Issue { get; set; }
public string IssueDetails { get; set; }
public Nullable<int> StorageId { get; set; }
public bool MeAsUser { get; set; }
}
采纳答案by Emanuele Greco
you 're using "dd'/'M'/'yyyy"
format initially,
您"dd'/'M'/'yyyy"
最初使用的是格式,
(DateTime.Now.ToString("dd'/'M'/'yyyy"))
and "dd/MM/yyyy"
format later,
并 "dd/MM/yyyy"
稍后格式化,
format: "dd/MM/yyyy"
this is not good.
Use the same format (I suggest "dd/MM/yyyy"
) in both cases.
情况不妙。在这两种情况下
使用相同的格式(我建议"dd/MM/yyyy"
)。
回答by Rolwin Crasta
Try this
试试这个
public class CreateDeviceInstance
{
public int Id { get; set; }
public int DeviceId { get; set; }
public string SerialNo { get; set; }
[Required(ErrorMessage="Data jest wymagana")]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public System.DateTime CreationDate { get; set; }
public Nullable<int> ProjectId { get; set; }
public bool Issue { get; set; }
public string IssueDetails { get; set; }
public Nullable<int> StorageId { get; set; }
public bool MeAsUser { get; set; }
}
回答by shajeer puzhakkal
DateTime.Now.ToString("dd/MM/yyyy")
回答by Andile Ngubane
"31/Marzec/2014" format : "dd/MM/yyyy"; do not use capital letters for specifying a month. The date format in bootstrap datepicker format : "dd/mm/yyyy" your result will change 31/03/2014
“31/Marzec/2014”格式:“dd/MM/yyyy”;不要使用大写字母来指定月份。bootstrap datepicker 格式中的日期格式:“dd/mm/yyyy”你的结果将在 31/03/2014 改变