Click here to Skip to main content
15,396,239 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an MVC5 Web application and am trying to get the JQuery DatePicker to work with DateTime EditorTemplates.

For the editor template I have;
HTML
@model System.DateTime?
@if (Model.HasValue)
{
    @Html.TextBox("", String.Format("{0:ddd MMM dd,yyyy}", Model.Value), new { @class = "datepicker" })
}
else
{
    @Html.TextBox("", String.Format("{0:ddd MMM dd,yyyy}", DateTime.Now), new { @class = "datepicker" })
}


the Javascript;

HTML
<script>
    $(document).ready(function () {
        $(".datepicker").datepicker({
            dateFormat: 'ddd MMM dd,yyyy',
            autoClose: true,
            todayBtn: true,
            todayHighlight: true
        });
    });
</script>


and finally the code;
HTML
<div class="col-lg-2">
    <label>Create Date</label><br/>
    @Html.EditorFor(model => model.CreateDate, new { @value =  @Model.CreateDate" })
 </div>


When I click on the control the DatePicker is displayed but;
1) It doesn't autoclose as it should.
2) If I don't select a date and click off somewhere else the current date is inserted.
3) When it first comes up the date is in the format I specified which is; "ddd dd MMM,yyyy" then when the date is inserted it is "MM/dd/yyyy" forever after.

What am I missing?

Thanks y'all
Posted
Updated 3-Mar-15 7:59am
v2

1 solution

Assuming this is the jQuery UI Datepicker widget[^], your client-side dateFormat is wrong. It should be:
D M dd, yy

The format is documented under the formatDate method[^].

There also doesn't appear to be any mention of the autoClose, todayBtn or todayHighlight options in the documentation[^].

Running the demos[^], I can't reproduce problems 1 or 2. You shouldn't need any custom setting to auto-close, and it shouldn't insert any date that you haven't explicitly selected.
   
Comments
Mike Hankey 3-Mar-15 13:58pm
   
Richard thanks for your reply but my stupidity prevails, I'm using Bootstrap.DatePicker.
Richard Deeming 3-Mar-15 14:02pm
   
There seem to be quite a few "Bootstrap Datepicker" plugins. :)

Is it the one from NuGet? https://github.com/eternicode/bootstrap-datepicker[^]

The options for that one seem to match your code. You still need to use the jQuery format specifiers, and I can't get the demo[^] to reproduce problems 1 or 2.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900