Remove the time part of a date in C #

4

This is not working right here:

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

The error is:

  

No overload for method 'ToString' takes 1 arguments

How do I remove the time part of a date?

    
asked by anonymous 08.12.2015 / 17:03

2 answers

6

First of all, the date format is wrong. It should be dd/MM/yyyy and not dd/mm/yyyy , so you're picking up the minutes instead of the month.

Font : Custom Date and Time Format Strings

The problem is that you are using Nullable DateTime and not DateTime normal. You should do it this way

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

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

DateTime novaData = _data.Value.Date; 

This will cause the novaData variable to be the same as the previous one, but with hours, minutes, and seconds reset.

    
08.12.2015 / 17:09
3

Use:

 _data.Value.ToShortDateString();
    
28.04.2016 / 01:53