0

I am creating an MVC5 project which utilizes scaffolding and has an EDM as a model. I want to use server-side code to remove the time portion of my datetime fields rather than parsing it with JQuery.

How do I achieve this?

ShowJX1990
  • 77
  • 2
  • 11
  • You cant use _server-side code_ to do this (your property is `DateTime` which has a 'time' component). If you want to display/edit it without the time, use `@Html.TextBoxFor(m => m.yourDate, "{0:d}")` which formats the value based on the format string. –  Aug 13 '15 at 01:21

2 Answers2

0

Try [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]

And if you dont want to validate it use a questionmark after the DateTime property like so public DateTime? Date{ get; set; }

        [DisplayName("Date of Birth:")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
        [Display(Order = 35)]
        public DateTime? CODoB { get; set; }

or you can just use editfor and display for templates here is a link

pool pro
  • 2,084
  • 12
  • 21
0

You can add EditorTemplate in the view folder, you need to create EditorTemplates folder and then create DateTime.cshtml and there you can pass the DateTime that came from Model to the default editor only the date portion like:@Html.EditorFor(x=>x.Date)

If that is not useful for you you need to tell me, where do you need to trim the time portion only in the view or also in database, and if so is your model code first or DataBase first approach

Ivelin Ivanov
  • 148
  • 3
  • 14