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.
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