Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

datetime

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.

like image 443
im useless Avatar asked May 25 '11 07:05

im useless


People also ask

How do I remove the timestamp from a date string?

string str = "1/15/2017 12:00:00 AM" str = DateTime. ParseExact(str, "mm/dd/yyyy hh:mm:ss tt", CultureInfo. InvariantCulture). ToString();

How to Remove time part from DateTime c#?

Remove Time Using the Built-in Date Property We get the same Date values, but for the time part, we get the default of 00:00:00. In other words, we got the Date component by simply erasing the values about time.


1 Answers

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.

like image 120
driis Avatar answered Sep 21 '22 10:09

driis