asp.net-mvc MVC 日期时间文本框格式问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17850830/
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
MVC DateTime Textbox Formatting Issue
提问by Austin Harris
I would like to do something like
我想做类似的事情
model.PickupDate.ToString("d")
But MVC4 is not liking that very much. PickupDateis a DateTimefield and I would like to strip off the time portion when displayed in the view, while keeping the new { id = "date1" }code which binds the TextBoxForto the javasript datepicker. How can I display the Date portion only in this instance?
但是 MVC4 不是很喜欢。 PickupDate是一个DateTime字段,我想在视图中显示时去掉时间部分,同时保留将new { id = "date1" }绑定TextBoxFor到 javasript datepicker的代码。如何仅在这种情况下显示日期部分?
@Html.TextBoxFor(model => model.PickupDate, new { id = "date1" })
回答by Kenneth Garza
this is how I would do this. Hope it helps
这就是我将如何做到这一点。希望能帮助到你
@Html.TextBoxFor(m => m.MyDateTime, new { Value = Model.MyDateTime.ToString("MM-dd-yyyy"), id = "MySuperCoolId"});
or in your case
或者在你的情况下
@Html.TextBoxFor(m => m.PickupDated, new { Value = Model.PickupDate.ToString("d"), id = "date1"});
回答by Yiding
This should work:
这应该有效:
@Html.TextBoxFor(model => model.SomeDateTime, "{0:MM/dd/yyyy}", new { id = "yourID" })
回答by James
Where you define your model you can supply the date formatting, in this example I have DD/MM/YYYY as my format, but you can do whatever format you need.
在您定义模型的地方,您可以提供日期格式,在本例中,我将 DD/MM/YYYY 作为我的格式,但您可以使用任何您需要的格式。
Model Code
型号代码
public class MyModel
{
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime SomeDateTime {get;set;}
}
Then your razor
然后你的剃须刀
@Html.EditorFor(model => model.SomeDateTime, new { id = "date1" })
回答by D.Rosado
Based on https://stackoverflow.com/a/13702321/787511, tested with JQueryUI datepicker.
基于https://stackoverflow.com/a/13702321/787511,使用 JQueryUI datepicker 进行测试。
@Html.TextBoxFor(model => model.PickupDate, "{0:d}", new { id = "date1" })
回答by Elvin Mammadov
If you make conversion only on the View, you can write this as follow
如果你只在View上进行转换,你可以这样写
@Html.TextBoxFor(model => model.SomeDateTime, String.Format("{0:MM/dd/yyyy}"))
回答by CrnaStena
Display will depend on culture. And while in most cases all other answers are correct, it did not work for me. Culture issue will also cause different problems with jQuery datepicker, if attached.
展示将取决于文化。虽然在大多数情况下所有其他答案都是正确的,但它对我不起作用。如果附加了 jQuery datepicker,文化问题也会导致不同的问题。
If you wish to force the format escape /in the following manner:
如果您希望/通过以下方式强制格式转义:
@Html.TextBoxFor(model => model.PickupDate, "{0:MM\/dd\/yyyy}", new { id = "date1" })
If not escaped for me it show 08-01-2010vs. expected 08/01/2010.
如果没有为我逃脱,它会显示08-01-2010与预期的对比08/01/2010。
Also if not escaped jQuery datepicker will select different defaultDate, in my instance it was May 10, 2012.
此外,如果没有转义 jQuery datepicker 将选择不同的 defaultDate,在我的实例中它是May 10, 2012.
回答by manoj kumar
@Html.TextBoxFor(model => model.Date, String.Format("{0:dddd, MMMM d, yyyy}",Model.Date), new { @class = "form-control"})

