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

3

The time is showing up for the user.

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

How are you looking:

Modelcode:

View code:

    
asked by anonymous 05.02.2014 / 18:32

4 answers

6

Instead of using the .ToString("dd/MM/yyyy") method, use .ToShortDateString() because it will convert to string regardless of the time, but the cool thing is that it will follow the rules set in the CultureInfo of your application. That is, if it is in Portuguese (pt-BR) it will be 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 get the value through Value .

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

There are many other methods in the DateTime class.

    
05.02.2014 / 18:49
1

As you are manually displaying the date by making ToString (), you simply specify a formatter:

item.DataInicial.ToString("dd/MM/yyyy")

MSDN Documentation on the ToString Method (string)

    
05.02.2014 / 18:42
1

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

@Html.ActionLink(item.DataInicial.Value.ToString("dd/MM/yyyy"), "Index", new { apuracaoSelecionada = item.ApuracaoIcmsID })
    
05.02.2014 / 18:41
0

Replace DateTime with a variable Date

    
05.02.2014 / 18:41