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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12109007/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:06:06  来源:igfitidea点击:

Unable to set datetime format in MVC 4 using data annotations

c#asp.net-mvcdatetimeasp.net-mvc-4datetime-format

提问by Sender

I will try everything but not working this(dd/MM/yyyy) date format, this always gate mm/dd/yyyy

我会尝试一切但不工作这个(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)

Using DateTime Template.

使用日期时间模板。

@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>

采纳答案by TrizZz

You missed 2 things here:

你在这里错过了两件事:

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

and you also need:

你还需要:

ApplyFormatInEditMode=true

Try to use this:

尝试使用这个:

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

回答by Darin Dimitrov

{0:dd/mm/yyyy}should be {0:dd/MM/yyyy}because mmmeans minutes, not months:

{0:dd/mm/yyyy}应该是{0:dd/MM/yyyy}因为mm意味着分钟,而不是几个月:

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

回答by Grey Wolf

If you use data-annotation. when display it good. but when edit mode. it still use server format (culture setting) DisplayFormat: like it name, only for display

如果您使用数据注释。当显示它好。但是当编辑模式时。它仍然使用服务器格式(文化设置) DisplayFormat:喜欢它的名字,只用于显示

There are some option to solve. - paste to server as string with your favorite format - write customer bindding - Setting globalization in web.config for Format : dd/MM/yyy i using setting bellow

有一些选项可以解决。- 以您喜欢的格式粘贴到服务器 - 编写客户绑定 - 在 web.config 中为格式设置全球化:dd/MM/yyy 我使用下面的设置

<system.web>
    <globalization culture="en-GB"/>
....