Remove Time in Datetime field

Asked

Viewed 1,336 times

3

inserir a descrição da imagem aqui

Dear friends, I need to present only the date in a Textboxfor field. I use this HTML Control because I use a Javascript mask that formats the field value at the time of the date input.

@Html.TextBoxFor(model => model.DtCadastro, htmlAttributes: new { @id = "DtCadastro", @name = "DtCadastro", @onkeyup = "javascript:Formatar(this.value, this.form.name, this.name, 'data');", @onchange = "javascript:Formatar(this.value, this.form.name, this.name, 'data');" })


[Display(Name = "Data do Cadastro:")]
[Required(ErrorMessage = "Informe a data do cadastro")]
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
public Nullable<System.DateTime> DtCadastro { get; set; }

2 answers

3


The simplest way of all is using only:

@Html.TextBoxFor(model => model.DtCadastro, "{0:dd/MM/yyyy}", new { @class = "form-control", placeholder = "Data de Cadastro" })

You don’t need any of those Javascript events you put in. It just solves.

  • It worked @Cigano .. Even with the mask it worked .. Thanks

2

For these cases, you can use a EditorTemplate

Create a folder called EditorTemplates inside the briefcase Shared Create a View DateTime.cshtml and put:

@model DateTime?
@Html.TextBox("", Model.HasValue && Model.Value != DateTime.MinValue ? Model.Value.ToShortDateString() : "", new { @class = "datepicker" })

When using a Datetime property use

@Html.EditorFor(model => model.Date,"DateTime")

The second option is to use the TextBoxFor

@Html.TextBoxFor(model => model.Date,string.format("{0:dd/MM/yyyy",Model.Date.Value))
  • It did not work, the following error occurred: "Index (based on zero) must be greater than or equal to zero and smaller than the length of the list of arguments". The field is being filled with the date of the day at the time of View loading.

  • @Trust what option you have made ?

  • The last one, Textboxfor..

  • @Gleisonfrancy fixed, is that your Datetime is nullable, so in the string.format has to have Model.Date.Value

  • unfortunately it did not work. I did several tests and again the error appeared: "The input string was not in an incorrect format".

  • Dude, send how you’re doing in your html helper

  • I removed the arrobas. It follows: Html.Textboxfor(model => model.Dtcadastro, string.Format("{0:dd/MM/yyyy", Model. DtCadastro.Value), htmlAttributes: new { id = "DtCadastro", name = "DtCadastro", onkeyup = "javascript:Formatar(this.value, this.form.name, this.name, 'data');", onchange = "javascript:Formatar(this.value, this.form.name, this.name, 'data');" })

  • That answer is almost right. I just feel like you’ve made it a little complicated.

  • @Ciganomorrisonmendez I spent what he wanted to know, formatting the date using Htmlhelper, however, it has more attributes of its own input...

Show 4 more comments

Browser other questions tagged

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