71

The line of code DateTime d = DateTime.Today; results in 10/12/2011 12:00:00 AM. How can I get only the date part.I need to ignore the time part when I compare two dates.

4
  • Today should also give a 0:00:00 result. Commented Oct 12, 2011 at 13:04
  • @digem : Do you mean that midnight is shown as 12AM? Could be. I don't think Today would actually return a culture-dependent value. Commented Oct 12, 2011 at 13:10
  • @Henk: in english cultures Today.ToString() returns that value ;) Commented Oct 12, 2011 at 13:10
  • @Henk: yes sorry, I was inaccurate. Actually the ToString() returns that value if you use for example en-US culture. It's just because "12.00 AM" is equal to the "00.00" in 24h format ;) (I know it seems strange for non-english people, I'm italian...) Commented Oct 12, 2011 at 13:12

5 Answers 5

125

DateTime is a DataType which is used to store both Date and Time. But it provides Properties to get the Date Part.

You can get the Date part from Date Property.

http://msdn.microsoft.com/en-us/library/system.datetime.date.aspx

DateTime date1 = new DateTime(2008, 6, 1, 7, 47, 0); Console.WriteLine(date1.ToString()); // Get date-only portion of date, without its time. DateTime dateOnly = date1.Date; // Display date using short date string. Console.WriteLine(dateOnly.ToString("d")); // Display date using 24-hour clock. Console.WriteLine(dateOnly.ToString("g")); Console.WriteLine(dateOnly.ToString("MM/dd/yyyy HH:mm")); // The example displays the following output to the console: // 6/1/2008 7:47:00 AM // 6/1/2008 // 6/1/2008 12:00 AM // 06/01/2008 00:00 
Sign up to request clarification or add additional context in comments.

2 Comments

var object = DateTime.Now; object.Date.ToString("MM/dd/yyyy HH:mm")
Here is another solution using String.Format, hope helps someone.
33

There is no way to "discard" the time component.

DateTime.Today is the same as:

DateTime d = DateTime.Now.Date; 

If you only want to display only the date portion, simply do that - use ToString with the format string you need.

For example, using the standard format string "D" (long date format specifier):

d.ToString("D"); 

3 Comments

+1: Sometimes the answer is just a . away
@Oded. No. It returns the same. 10/12/2011 12:00:00 AM.
@Rauf - You can't get rid of that portion. You can choose not to display it though.
16

When comparing only the date of the datatimes, use the Date property. So this should work fine for you

datetime1.Date == datetime2.Date 

2 Comments

this is the best answer.
+1, this is relevant for comparing dates. other answers are for displaying them
13
DateTime d = DateTime.Today.Date; Console.WriteLine(d.ToShortDateString()); // outputs just date 

if you want to compare dates, ignoring the time part, make an use of DateTime.Year and DateTime.DayOfYear properties.

code snippet

DateTime d1 = DateTime.Today; DateTime d2 = DateTime.Today.AddDays(3); if (d1.Year < d2.Year) Console.WriteLine("d1 < d2"); else if (d1.DayOfYear < d2.DayOfYear) Console.WriteLine("d1 < d2"); 

Comments

6

you can use a formatstring

DateTime time = DateTime.Now; String format = "MMM ddd d HH:mm yyyy"; Console.WriteLine(time.ToString(format)); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.