Date format in Asp.net mvc

Asked

Viewed 3,360 times

2

I’m having problems with the date format on en in an Asp.net mvc application. Where it displays error if the date is in the form dd/MM/yyyy HH:mm, and only let it pass if the date is in the formed MM/dd/yyyy HH:mm.

Model

 [Display(Name = "Data de Publicação", Description = "Selecione uma data futura para agendar uma publicação")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy HH:mm}", ApplyFormatInEditMode = true)]
        public System.DateTime DataPublicacao { get; set; }

View

<div class="form-group">
            @Html.LabelFor(model => model.DataPublicacao, htmlAttributes: new { @class = "control-label col-xs-12 col-sm-12 col-md-12" })
            <div class="col-xs-12 col-sm-12 col-md-12">
                @Html.TextBoxFor(m => m.DataPublicacao, "{0:dd/MM/yyyy HH:mm}", htmlAttributes : new { @class = "form-control date-timepicker"})
                @Html.ValidationMessageFor(model => model.DataPublicacao, "", new { @class = "text-danger" })
            </div>
        </div>

Javascript of startup of datetimepicker

$('.date-timepicker').datetimepicker({
            locale: 'pt-br',
            keepOpen: true,
            showTodayButton: true,
            format: 'DD/MM/YYYY HH:mm'
});

Webconfig:

<system.web>
    <globalization culture="pt-BR" uiCulture="pt-BR" />
    <compilation debug="true" targetFramework="4.5.2" />
    <httpRuntime targetFramework="4.5.2" />
  </system.web>

Upshot:

inserir a descrição da imagem aqui

  • @Renan unfortunately the question which marked as duplicate, none of the answer was possible to solve my problem.

  • Erico, it would be good to then edit your question to indicate this. Link to the other question and say that no answer in it solved your case.

1 answer

3

I had the same problem and what helped me to solve was this blog post here: MVC 3: Validation for crops such as en

In my project I am using a newer version of ASP.NET MVC, but still the problem was solved by adding the validation script.

As suggested by colleague David, follows a summary:

"To support date validation for cultures such as en-BR, it is necessary to add a script with specific validation methods."

You can do this as follows: create a methods_pt.js file within your project’s Scripts folder and paste the following content into it:

/*
 * Localized default methods for the jQuery validation plugin.
 * Locale: PT_BR
 */
jQuery.extend(jQuery.validator.methods, {
    date: function(value, element) {
        return this.optional(element) || /^\d\d?\/\d\d?\/\d\d\d?\d?$/.test(value);
    }
});

Keep the tag on your web.config <globalization culture="pt-BR" uiCulture="pt-BR" /> as you showed in your question.

And finally, on the pages where you need to use validation, include the scripts as in the following code:

<!-- Scripts de validação -->
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"  type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<!-- Correção de funções de validação específicas para a cultura pt-BR -->
<script src="@Url.Content("~/Scripts/methods_pt.js")" type="text/javascript"></script>
  • Unfortunately, I still have the same problem. The date is still invalid when it is in the formed dd/MM/yyyy HH:mm.

  • When I had the same question problem, that’s what solved me too.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.