7

How do I prevent the time from showing up when using the jQuery .datepicker()? Below is the section of the model for the two attributes (date taken and expiration date)

    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
    [DisplayName("Expiration Date")]
    public string ExpirationDate { get; set; }

    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
    [DisplayName("Date Taken")]
    public string DateTaken { get; set; }

This works fine using the @Html.EditorFor control on the view I'm working on. However, when I change that control from this:

 @Html.EditorFor(model => model.DateTaken)

to this:

@Html.TextBoxFor(m => m.DateTaken, new { id = "dtkn" })

in order to get this to work (label which control has the datepicker applied to it):

<script>
    $(function () {
        $("#dtkn").datepicker('setDate');
        $("#exp").datepicker('setDate');
    });

    $.datepicker.setDefaults({
        buttonImageOnly: true,
        buttonImage: '<%=Url.Content("~/Content/images/magnify.gif") %>',
        defaultDate: -1,
        gotoCurrent: true,
        prevText: "<< Prev",
        nextText: "Next >>"
    });
</script>

suddenly the time begins to show up. Any suggestions?

EDIT

I have tried the following formats and none will work:

$("#dtkn").datepicker({ dateFormat: 'dd-mm-yy' }).val();
$("#dtkn").datepicker({ dateFormat: 'yy-mm-dd' });
$("#dtkn").datepicker('getDate').format('yyyyMMdd');

and this:

$("#dtkn").datepicker('getDate').datepicker('dd/mm/yy');
$('#dtkn').datepicker('option', { dateFormat: 'd MM y' });

Here is the error I'm seeing the Chrome debugging console:

Uncaught TypeError: Cannot call method 'datepicker' of null 
2
  • Duplicate stackoverflow.com/questions/1328025/… Commented Mar 29, 2013 at 18:04
  • Tried those solutions without any luck. See edits. Commented Mar 29, 2013 at 19:36

3 Answers 3

15

Edit: MVC4 users can use this

You want this overload

@Html.TextBoxFor(m => m.DateTaken, "{0:MM/dd/yyyy}", new { id="dtkn" })

If you're willing to give up on the helper just to it manually

<input type="text" value="@Model.DateTaken.ToString("MM/dd/yyyy")" id="dtkn" />
Sign up to request clarification or add additional context in comments.

3 Comments

I get the error No overload for method 'TextBoxFor' takes 3 arguments
this seems to be the most sensitive approach if you don't want to mess with existing markup and/or view model
4

By using @Html.TextBox as opposed to @Html.TextBoxFor I was able to find an overload that works.

@Html.TextBox("DateTaken", Model.DateTaken.ToString("MM/dd/yyyy"), new { id = "dtkn" })

Comments

0

@Html.TextBoxFor(m => m.DateTaken,"{0:d/M/yyyy}", new { id = "dtkn" })

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d/M/yyy}")] [DisplayName("Date Taken")] public string DateTaken { get; set; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.