0

My MVC datepicker field is giving me the year twice. So today's date appears like this:

09/16/20152015

I'm wondering what's going wrong. Here's a section from my create view:

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

I have a custom editor:

@model DateTime?
@{
    var value = "";
    if (Model.HasValue) {
        value = String.Format("{0:d}", Model.Value.ToString());
    }
}
@Html.TextBox("", value, new { @class = "datepicker", type = "text" })

My Javascript Script.js file has this:

$(document).ready(function () {
    $(".datepicker").datepicker({
        dateFormat: "mm/dd/yyyy",
        autoclose: true
    });
});

Any ideas what I'm doing wrong?

M Kenyon II
  • 4,136
  • 4
  • 46
  • 94

1 Answers1

1

The default format is mm/dd/yy which shows the date like 09/16/2015, So you don't need to change it.

    $(function() {
        $(".datepicker").datepicker({
            autoclose: true
        });
    });
Sirwan Afifi
  • 10,654
  • 14
  • 63
  • 110