1

I have used Jquery UI datepicker. My model looks like:

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

For displaying in view

@Html.TextBoxFor(x => x.endDate, new { @class = "datepicker" })

Script

<script type="text/javascript" language="javascript">
    $(document).ready(function () {
        $(function () {
            $(".datepicker").datepicker({
                dateFormat: "mm/dd/yy",
                changeMonth: true,
                changeYear: true
              });
        });
    });
</script>

When the date time is displayed it looks like 09-Nov-12 12:00:00 AM I just want to show date without time.Thankx for the help.

S.p
  • 1,059
  • 3
  • 15
  • 27

6 Answers6

3

You can parse you date in desired format via datetime picker. In the javascript try this:

$(document).ready(function(){
    $("#endDate").val($.datepicker.parseDate('mm/dd/yy', $("#endDate").val()));

    // Existing stuff
});

Alternately you can use $.datepicker.formatDate.

Example :

$("#endDate").val($.datepicker.formatDate('dd-M-yy', new Date($("#endDate").val())));

DEMO

Good Luck !!

Kundan Singh Chouhan
  • 13,952
  • 4
  • 27
  • 32
2
[DisplayName("Start Date")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime StartDate { get; set; }

Date only from TextBoxFor()

Community
  • 1
  • 1
JConstantine
  • 3,980
  • 1
  • 33
  • 46
2

Use this

@Html.TextBoxFor(x => x.HasValue ? x.Value.ToString("MM/dd/yy") : string.Empty, new { @class = "datepicker" })

You can pick any format you like from here: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

prashanth
  • 2,059
  • 12
  • 13
1

You can use either of the methods above, e.g. format it as an output, but do you really need to return DateTime property when you are displaying it? Isn't it more efficient to display @Html.TextBoxFor(x => x.endDate.Date?

MaxDataSol
  • 358
  • 1
  • 2
  • 18
1

For DateTime I prefer use EditorTemplate:

@model DateTime?

@Html.TextBox("", (Model.HasValue ? Model.Value.ToString("MM/dd/yyyy") : string.Empty), new { @class = "datepicker" })
webdeveloper
  • 17,174
  • 3
  • 48
  • 47
0

I used EditorFor instead of TextBoxFor

@Html.EditorFor(x => x.endDate, new { @class = "datepicker" })

in the view which is working fine.

S.p
  • 1,059
  • 3
  • 15
  • 27