-5

I have generated Controller and View using add scaffolding in Visual Studio 2017, but the datetime input field need to enter the date time manually instead of datetime picker. I have tried several ways but still not able to implement datetime picker.

    [DataType(DataType.DateTime)]
    public DateTime EnrollmentDate { get; set; }        

The datetime can be selected when I added

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

but I need hour and minutes as well but this is not working.

    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd-hh-mm-tt}", ApplyFormatInEditMode = true)]
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
psps
  • 11
  • 3
  • Possible duplicate of [Display DateTime value in dd/mm/yyyy format in Asp.NET MVC](https://stackoverflow.com/questions/18288675/display-datetime-value-in-dd-mm-yyyy-format-in-asp-net-mvc) – Aswin Apr 11 '18 at 04:40
  • _"not working"_ tells us nothing. – Aluan Haddad Apr 11 '18 at 04:40

2 Answers2

2

DateTime picker can be implemented using jQuery's date time picker. Or if you want an inbuilt MVC datetime picker, modify your code as :

Field:

   [DataType(DataType.Date)]
   public DateTime EnrollmentDate { get; set; }  

and then in view

  <div class="form-group">
            @Html.LabelFor(model => model.EnrollmentDate , htmlAttributes: new {@class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.EnrollmentDate , new {htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.EnrollmentDate , "", new { @class = "text-danger" })
            </div>
        </div>

which will then render the date picker for you.

Sweta Nair
  • 146
  • 6
  • I was trying to select date and time but I think jQuery does not have datetime picker so I just use date picker – psps Apr 12 '18 at 11:19
0

I believe what you're looking for is this

[DisplayFormat(DataFormatString = "{0:yyyy-MM-ddThh:mm}", ApplyFormatInEditMode = true)]

Note the capital T

Produces 20/01/2020 12:04 PM

Possam
  • 363
  • 4
  • 18