Separate the time component from the DateTime object
Question:
You need to remove the time part from the DateTime object without using the string conversion method. Dates should be kept in object form.
Solution:
To do this, use the Date property of the DateTime class:
<code class="language-csharp">var dateAndTime = DateTime.Now; var date = dateAndTime.Date;</code>
The date variable will now hold the date with the time part set to 00:00:00.
Example:
Consider the following example:
Input: 06/26/2009 00:00:00:000
Use the Date attribute:
<code class="language-csharp">var dateAndTime = DateTime.Parse("06/26/2009 00:00:00:000"); var date = dateAndTime.Date; Console.WriteLine("Date: " + date);</code>
Output: Date: 06/26/2009 00:00:00
As you can see, the time part has been removed from the DateTime object, leaving only the date.
The above is the detailed content of How to Separate the Date from the Time in a DateTime Object?. For more information, please follow other related articles on the PHP Chinese website!