Remove Time in the DateTime field

3

Dear,IneedtodisplayonlythedateinaTextBoxForfield.IusethisHTMLControlbecauseIuseaJavaScriptmaskthatformatsthefieldvalueatthetimeoftheinputofthedate.

@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; }
    
asked by anonymous 17.11.2015 / 12:59

2 answers

3

The simplest way is to use only:

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

You do not need any of those JavaScript events that you have placed. That alone solves it.

    
17.11.2015 / 15:07
2

For these cases, you can use a EditorTemplate

Create a folder named EditorTemplates within the Shared Create a View DateTime.cshtml and enter:

@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 TextBoxFor itself

@Html.TextBoxFor(model => model.Date,string.format("{0:dd/MM/yyyy",Model.Date.Value))
    
17.11.2015 / 13:07