A.Alshaikhli
A.Alshaikhli

Reputation: 437

DataFormatString for DateTime with mvc5

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

Upvotes: 0

Views: 1262

Answers (1)

Arkadiusz Raszeja
Arkadiusz Raszeja

Reputation: 882

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>
}

Upvotes: 1

Related Questions