Skip to content
Fix Code Error

How to remove time portion of date in C# in DateTime object only?

March 13, 2021 by Code Error
Posted By: Anonymous

I need to remove time portion of date time or probably have the date in following format in object form not in the form of string.

06/26/2009 00:00:00:000

I can not use any string conversion methods as I need the date in object form.

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

Solution

Use the Date property:

var dateAndTime = DateTime.Now;
var date = dateAndTime.Date;

The date variable will contain the date, the time part will be 00:00:00.

Answered By: Anonymous

Related Articles

  • Apache server keeps crashing, "caught SIGTERM, shutting…
  • Get a list of dates between two dates
  • Sort table rows In Bootstrap
  • Regex needed to match a proper currency format of whole…
  • How to print a number with commas as thousands separators in…
  • Sort a two dimensional array based on one column
  • Change in pandas Series.tz_convert behaviour in v1.X (or…
  • Is it possible to style different sections of…
  • Converting year and month ("yyyy-mm" format) to a date?
  • Tips for Aggregating MongoDB Time Trend Data over groups…

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How to check if any value is NaN in a Pandas DataFrame

Next Post:

How to get line count of a large file cheaply in Python?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Get code errors & solutions at akashmittal.com
© 2022 Fix Code Error