0

I`m new to asp.Net mvc5. I have a problem with DateTime formatting. I need to remove time from Date.

20/01/2015 12:00:00

Needs to be like this :

20/01/2015

I Tried this :

var dateOnly = date1.Date;

But it still get the time with date.

Dharmesh Porwal
  • 1,406
  • 2
  • 12
  • 21
wajira000
  • 379
  • 5
  • 16

2 Answers2

1

If you want to format the date in a string, you can specify a custom format which excludes the time:

DateTime date = DateTime.Now;
string dateString = date.ToString("dd/MM/yyyy");

By default, .NET will include the time too when converting a DateTime object to a string.

Henk Mollema
  • 44,194
  • 12
  • 93
  • 104
1

You'll always have some time in a DateTimeobject. But you can format the string output to have what you need. There is a difference between the inner data hold be the C# object (ie with time, even if 00:00) and the way you print it on screen.

Try:

var dateAsString = date1.ToString("dd/MM/yyyy")

Everything is in the MSDN Documentation.

Askolein
  • 3,250
  • 3
  • 28
  • 40