How to remove a temporary part of a date in C # only in a DateTime object

I need to remove the time part of the time of the date, or possibly have the date in the following format as an object not in the form of a string.

06/26/2014 00: 00: 00: 000

I cannot use any string conversion methods since I need a date as an object.

I tried first converting the date to a string, removing a specific time date from it, but it adds 12:00:00 AM as soon as I again convert it to a DateTime object.

+5
source share
4 answers

You cannot create a DateTime object without time. There is always time in it.

If you want to display it without a temporary part, you can use the line for this:

date.ToString("MM/dd/yyyy"); 

Standard Date and Time Format Strings
Custom Date and Time Format Strings

As others have said, you can access date.Date to get a value indicating any specific time information, but it will still have a time of 00:00.

+7
source

You can use the Date property of a DateTime object to get only one day.

 DateTime dateOnly = date1.Date; 

If you need a Date string from a DateTime object, use the ToString method, giving it a format. You can learn more about custom date formats in this MSDN article.

 string strDate = date.ToString("MM/dd/yyyy"); 
+4
source

I need to remove the time part of the date time ...

It's impossible. You cannot use a DateTime instance without time information. For example, you cannot have an instance of DateTime as soon as 06/26/2014 or only 00:00:00:000 .

If you want to get only the date in your instance, you can use the DateTime.Date property .

Gets the date component of this instance.

But still, this .Date property sets it to midnight.

Remember that DateTime has no implicit format. It just has a date and time value. The concept of the format applies only when you want to show it as a string .

In the view part, you should use a string view like:

 yourDateTime.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture); 
+2
source
 var dateAndTime = DateTime.Now; var date = dateAndTime.Date; 

The date variable will be indicated in the date variable, the time will be 00:00:00.

+2
source

Source: https://habr.com/ru/post/1209520/


All Articles