"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to remove the time part from the DateTime object in C#?

How to remove the time part from the DateTime object in C#?

Posted on 2025-04-13
Browse:598

How to Remove the Time Component from a DateTime Object in C#?

Stripping Time from a C# DateTime Object

This guide demonstrates how to remove the time component from a DateTime object in C# without converting it to a string.

The Solution:

Leverage the Date property of the DateTime structure:

DateTime dateAndTime = DateTime.Now;
DateTime dateOnly = dateAndTime.Date;

The dateOnly variable now contains the date portion of dateAndTime, with the time set to midnight (00:00:00). This maintains the data type as a DateTime object, avoiding string conversions.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3