Question

I'm working with ASP.NET MVC 4 and bootstrap-datepicker. I'm trying to format the date as dd-mm-yyyy.

My model:

[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime? DataInicio_P_C { get; set; }

View (input and ValidationMessageFor):

<div class="input-append date" id="datepicker1" data-date="dateValue: Customer.DateOfBirth" data-date-format="dd-mm-yyyy">
                    @Html.TextBoxFor(model => model.DataAssin_C, new { @class = "form-control input-sm add-on", id = "DataAssin_C" })
            </div>

.

<td colspan="2">@Html.ValidationMessageFor(model => model.DataAssin_C, "", new { @class = "error-input" })</td>

Call datepicker:

$('#datepicker').datepicker({ format: 'dd-mm-yyyy' });

But every time I get the error "The field DataAssin_C must be a date", only validate date in format mm-dd-yyyy or mm/dd/yyyy when i want dd-mm-yyyy

Was it helpful?

Solution

Controller:

if (DateTime.Parse(form["DataAssin_C2"]) != null)
            {
                dados.DataAssin_C = DateTime.Parse(form["DataAssin_C2"]);
                dados.DataAssin_C = DateTime.Parse(dados.DataAssin_C.Value.ToString("yyyy-MM-dd"));
            }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top