SventoryMang
SventoryMang

Reputation: 10478

jQueryUI datepicker with MVC and knockout js

Okay I've tried pretty much everything in the other answers I've seen here.

I am using jQueryUI datepicker on my MVC form, which also uses knockout. I want to only display the date, not the time.

Heres my markup:

<div class="editor-label">
        @Html.LabelFor(model => model.StartDate.Date)
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.StartDate.Date, new { @class = "date-picker", id = "StartDate", data_bind = "value: StartDate" })
        @Html.ValidationMessageFor(model => model.StartDate.Date)
    </div>

And then in my model I've set the datatype:

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

And in the datepicker jquery in document.ready I have the format set:

$(".date-picker").datepicker({ dateFormat: "dd/mm/yy" });

Yet when the page loads, it still displays the time (12:00 AM) along with the date. How can I get rid of this??

Upvotes: 0

Views: 617

Answers (3)

Mike Wade
Mike Wade

Reputation: 1746

Make your date value output as StartDate.ToShortDateString()

Upvotes: 0

Jason Haley
Jason Haley

Reputation: 3800

Try adding this afterwards:

$(".date-picker").datepicker("setDate", new Date("@Model.StartDate"));

Upvotes: 0

Scottyjones
Scottyjones

Reputation: 36

Try setting the type of textbox to date. Using the attributes will only work with the editor not textbox.

Upvotes: 1

Related Questions