Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove time portion of date in C# in DateTime object only [closed]

Tags:

c#

.net

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/2014 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 date to string, remove the time specific date from it, but it adds 12:00:00 AM as soon as I convert it to DateTime object back again.

like image 703
sonali Avatar asked Dec 19 '14 07:12

sonali


2 Answers

You can't create a DateTime object without a time in it. It always has some time in it.

If you want to output it without a time portion, you can use a format string to do so:

date.ToString("MM/dd/yyyy");

Standard Date and Time Format Strings
Custom Date and Time Format Strings

As others have said, you can access date.Date to get a value with any specific time information omitted, but that will still have a time of 00:00 AM.

like image 140
JLRishe Avatar answered Sep 30 '22 06:09

JLRishe


You can use the Date property of DateTime object to get only day.

DateTime dateOnly = date1.Date;

If you want string of Date from the DateTime object then use the ToString method by providing it the format. You can read more about custom date format in this MSDN article.

string strDate =  date.ToString("MM/dd/yyyy");
like image 39
Adil Avatar answered Sep 30 '22 04:09

Adil