c# - 无法使用数据注释在 MVC 4 中设置日期时间格式

标签 c# asp.net-mvc datetime asp.net-mvc-4 datetime-format

我会尝试一切,但不会使用这种 (dd/MM/yyyy) 日期格式,这总是门 mm/dd/yyyy

[Display(Name = "Release Date")]
[DataType(DataType.DateTime)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public Nullable<DateTime> release_date { get; set; }

Razor View

@Html.EditorFor(model => model.release_date)

使用日期时间模板。

@model Nullable<System.DateTime>
@if (Model.HasValue)
{
    @Html.TextBox("", String.Format("{0:dd/MM/yyyy}", Model.Value))
}
else
{
    @Html.TextBox("", String.Format("{0:dd/MM/yyyy}", DateTime.Now))
}
@{
    string name = ViewData.TemplateInfo.HtmlFieldPrefix;
    string id = name.Replace(".", "_");
}
<script type="text/javascript">
    $(document).ready(function () {
        $("#@id").datepicker({
                dateFormat: "dd/mm/yy",
                showStatus: true,
                showWeeks: true,
                highlightWeek: true,
                numberOfMonths: 1,
                showAnim: "scale",
                showOptions: {
                    origin: ["top", "left"]
                }
          });
    });
</script>

最佳答案

你在这里错过了两件事:

DataFormatString="{0:dd/MM/yyyy}"; //It is dd/MM/yyyy instead of dd/mm/yyyy

你还需要:

ApplyFormatInEditMode=true

尝试使用这个:

[Display(Name = "Release Date")]
[DataType(DataType.Date), DisplayFormat( DataFormatString="{0:dd/MM/yyyy}", ApplyFormatInEditMode=true )]
public Nullable<DateTime> release_date { get; set; }

关于c# - 无法使用数据注释在 MVC 4 中设置日期时间格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12109007/

相关文章:

MYSQL 将日期时间四舍五入为 15 分钟

c# - 在MVC中读取excel文件数据

asp.net-mvc - Azure AD SAML 身份验证 SP 启动

java解码LocalDateTime

c# - 如何替换静态 ObservableCollection 以便以 MVVM 方式在所有窗口上访问它

c# - Composite 的简单注入(inject)器注册,结合了开放和封闭的通用实现

C#/.NET - 最小并行化快速排序导致性能下降

c# - 在 CaSTLe Windsor 中调用 ResolveAll 时的顺序

asp.net-mvc - 报告定义无效。详细信息 : The report definition has an invalid target namespace

Java 字符串到日期到不同字符串格式