Remove the time part of a date in c#

Asked

Viewed 4,976 times

4

This is not working:

DateTime? _data = calDataExclusao.Date;
string nova_data = _data.ToString("dd/mm/yyyy");

The mistake is:

No Overload for method 'Tostring' takes 1 Arguments

How I remove the time part of a date?

2 answers

7


First of all, the date format is wrong. It should be dd/MM/yyyy and not dd/mm/yyyy, this way you’re taking the minutes instead of the month.

Source: Custom Date and Time Format Strings


The problem is you’re wearing one Nullable DateTime and not a DateTime normal. You should do it this way

string nova_data = _data.Value.ToString("dd/MM/yyyy");

This to convert the DateTime for string, if you really want to remove the time part of a date you must do

DateTime novaData = _data.Value.Date; 

This will make the variable novaData the same date as before, but with hours, minutes and seconds reset.

3

Use:

 _data.Value.ToShortDateString();

Browser other questions tagged

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