0

i have searched and tried to do it in many ways (changing the DataFormatString) but none of them are working. I'm also using datepicker for my Date fields.

 [Required]
 [Column(TypeName = "date")]
 [DataType(DataType.Date)]
 [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd.MM.yy}")]
 public DateTime? Date { get; set; }

and this is my view

    @Html.LabelFor(model => model.Server_Cadworker.Date, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @{ Html.EnableClientValidation(false); }

            @Html.EditorFor(model => model.Server_Cadworker.Date, new { htmlAttributes = new { @class = "form-control" } })
                @{ Html.EnableClientValidation(true); }
                @Html.ValidationMessageFor(model => model.Server_Cadworker.Date, "", new { @class = "text-danger" })
            </div>

any one has answer for this issue?

Thanks

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
A.Alshaikhli
  • 387
  • 2
  • 8
  • 18
  • What is the question? You want to display date in some special way, or it just does not working at all? – Arkadiusz Raszeja Apr 20 '17 at 11:32
  • it shows my date like "{0:MM.dd.yy}" and i want it to be like this {0:dd.MM.yy} – A.Alshaikhli Apr 20 '17 at 11:35
  • Are you using a jquery datepicker plugin (if so your `DataType` and `ApplyFormatInEditMode` are pointless. If you generating a HTML-5 datepicker (which is only supported in Chrome and Edge), then it needs to be `"{0:yyyy-MM-dd}"` - ISO format, and the date is displayed in the users culture - that is the whole point of the control –  Apr 20 '17 at 12:19
  • @StephenMuecke that is good clarification. Actually, i am using HTML-5 datepicker that why i couldn't change it. I will try to use other datepicker. thanks. – A.Alshaikhli Apr 20 '17 at 14:15

1 Answers1

1

To display date in dd.MM.yy just use:

myDate.ToString(dd.MM.yy);

There is no sutch thing as date format, you can just display it in different way :)

EDIT:

@Html.TextBoxFor(Model => Model.Date, new { @Value = Model.Date.ToString("dd.MMM.yy") })

or

@Html.TextBoxFor(Model => Model.Date, "{0:dd.MM.yy}")

As a full list:

@foreach (var item in Model)
{
    <div>
        @item.Date.ToString("dd.MMM.yy")
        <hr />
    </div>
}