How to hide the time of a "Datetime?" field in the user interface?

Asked

Viewed 7,116 times

3

The time is appearing for the user.

My question: it is possible to hide the time and show only the date?

How are you showing: interface

Model code:

model

View code:

view

  • 1

    If possible place the source code instead of an image of it.

4 answers

6


Instead of using the method .ToString("dd/MM/yyyy") use the .ToShortDateString(), because it will do the conversion to string disregarding the time, but the coolest is that it will follow the rules defined in CultureInfo of your application. That is, if you are in Portuguese (en-BR) dd/MM/yyyy, but if it is in English (en-US) it will be mm/dd/yyyy.

item.DataInicial.ToShortDateString() // somente a data (como solicitou)
item.DataInicial.ToShortTimeString() // somente a hora

If your field is DateTime? (nullable), you will need to pick up the value through the Value.

if(item.DateInicial.HasValue){ item.DateInicial.Value.ToShortDateString(); }

There are many other methods in the class DateTime.

1

Try using String Format, example .ToString("dd/MM/yyyy"), in the case:

@Html.ActionLink(item.DataInicial.Value.ToString("dd/MM/yyyy"), "Index", new { apuracaoSelecionada = item.ApuracaoIcmsID })
  • Opa, it worked, as the field is "Datetime?" I needed to put (item.DataFinal.Value.Tostring("dd/MM/yyyy") the "Value" tbm, thank you.

  • True! I’d forgotten the Nullable, corrected in the example.

1

  • Opa, it worked, as the field is "Datetime?" I needed to put (item.DataFinal.Value.Tostring("dd/MM/yyyy") the "Value" tbm, thank you.

0

Replace the variable DateTime by a variable Date

Browser other questions tagged

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