Home >Backend Development >C++ >How to Separate the Date from the Time in a DateTime Object?

How to Separate the Date from the Time in a DateTime Object?

Barbara Streisand
Barbara StreisandOriginal
2025-01-21 09:38:39465browse

How to Separate the Date from the Time in a DateTime Object?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn