在 ASP.NET MVC C# 中更改日期格式

Changing date format in ASP.NET MVC C#

如何更改 asp.net 模型视图控件的日期格式?

谁能帮帮我?

是否有机会在不创建新日期格式的情况下更改日期格式class

public class Story
{
    [ScaffoldColumn(false)]
    public int ID { get; set; }
    [Required(ErrorMessage = "The Title is required")]
    public string Title { get; set; }
    public DateTime? PostDate { get; set; }
    [Required(ErrorMessage = "The Text is required")]
    public string Text { get; set; }

    public virtual string Image { get; set; }
}

您可以对字段应用 data annotation。例如,如果您想要英国格式:

[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime? PostDate { get; set; }

如果您使用标准 EditorFor,这将反映在显示和编辑模式中。

[编辑]

在您的 cshtml 中,您可以使用:

@DisplayFor(model => model.PostDate)