1

How can I change the date format for asp.net model view control?

Can any one help me out?

Is there any chance to change the date format with out creating a new 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; }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0

You can apply a data annotation to the field. For example if you want UK format:

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

This will be reflected in the display and edit modes if you are using the standard EditorFor.

[Edit]

In your cshtml, you can then use:

@DisplayFor(model => model.PostDate)
Community
  • 1
  • 1
Jane S
  • 1,417
  • 16
  • 21