-1

I cannot find any elaboration on why the Date property of the DateTime object is defaulted to midnight. I know that it is, through my own work as well as MSDN, but I am trying to understand the reasoning behind this. I cannot find any articles elaborating on why this is so.

Edit: To elaborate on some of the points being asked in comments.

string a = "2014-10-22 09:00 PM";
DateTime d = DateTime.Parse(a);

In this example I would have assumed it would default to 21:00:00.000- again I know it does not.

Nyra
  • 859
  • 1
  • 7
  • 27
  • 6
    I think you mean why the time property is midnight? Probably because it's the first time of the day. Regardless, what time would you propose instead? – jamesSampica Oct 22 '14 at 13:22
  • 1
    `DateTime.Date` is for "getting" `Date` out of Date and Time. .Net `DateTime` has both date and time part set. Therefore it makes sense to set time to `00:00:00` for just `Date` – Habib Oct 22 '14 at 13:22
  • 1
    Maybe because at midnight the time part is `0`: hour, minute and second are equal to `0`. – Dmitry Oct 22 '14 at 13:22
  • 6
    What on Earth else would you have it default to if not `00:00:00`? – Matt Burland Oct 22 '14 at 13:24
  • From the [docs](http://msdn.microsoft.com/en-us/library/system.datetime.date(v=vs.110).aspx) "Because the DateTime type represents both dates and times in a single type, it is important to avoid misinterpreting a date returned by the Date property as a date and time." – Habib Oct 22 '14 at 13:25
  • 2
    I'am more curious what could be else? :) – Renatas M. Oct 22 '14 at 13:25
  • 1
    For the same reason that an `int` defaults to `0` – Rufus L Oct 22 '14 at 13:27
  • I don't understand the downvotes or being put on hold- it is not an opinion, MSDN clearly sets it to 00:00:00.000 even if you set the Time value- I am looking for a direct answer as to why. For comments about "what else would it be?" I would understand if you don't pass it a time value, but in edit of post, if you were to look at that object, d.TimeOfDay is set to 21:00:00... So again not sure about the downvotes or opinion- there is no opinion on this- there is hypothetical answers and then there is the answer- and I cannot find *the answer* per MSDN. – Nyra Oct 22 '14 at 13:32
  • @alykins: because the `Date` property truncates the time potion of the `DateTime` but a `DateTime` must have a time portion. So it's midnight since that's the beginning of the day. If you actually want to display it without time, use `d.ToShortDateString()`. But then we're talking about strings. – Tim Schmelter Oct 22 '14 at 13:33
  • @TimSchmelter Yes, I get it truncates it, but why??? It truncates it just to default it to something I didn't set? That makes no sense. That's like asking someone what time they want to wake up, and then completely ignoring that time and putting in whatever you want. I get it if there is no time supplied, but the time piece is there. Can you explain why it's just ignored when supplied? – Nyra Oct 22 '14 at 13:34
  • 1
    @alykins: `Date` means the day, the same way `DateTime.Today` returns the current date's `DateTime` at midnight (as opposed to `DateTime.Now`). What do you expect it to return instead? A `DateTime` is a struct which always contains a time even if it's set to `0:00:00`. – Tim Schmelter Oct 22 '14 at 13:36
  • Because DateTime.DATE returns only DATE part. If you want for example compare 2 dateTimes(with different only time) `dateTime1` wont be equal to `dateTime2` but dateTime1.Date will be equat to dateTime2.Date. You want to compare only dates not dates and times. – Renatas M. Oct 22 '14 at 13:37
  • If you are worried about the time, why are you looking at just the `Date` portion of the object? – eddie_cat Oct 22 '14 at 13:37
  • 1
    @alykins: I feel that this question has been grossly misunderstood. The real reason here is that DateTime is internally stored as a *number*; the number of nanoseconds that have passed since 1 January, 0001. Give it some thought and you'll figure out what's going on. – dotNET Oct 22 '14 at 13:38
  • It's not something I'm worried about per se, it's something I noticed while I was working with some DateTime objects and noticed that top-level it displayed 12:00 AM. @dotNET Yes, I think everyone is misunderstanding what I am asking- I am thinking about your comment now to try and see it on my own. – Nyra Oct 22 '14 at 13:39
  • @alykins: _"That's like asking someone what time they want to wake up, and then completely ignoring that time and putting in whatever you want."_ No, if he wants to wake up at a given `DateTime` he should not give you his `DateTime`'s `Date` property since that ignores the time by definition. Instead he should give you the `DateTime` directly. Maybe you're **coming from VB.NET** where [`Date` is an alias for `DateTime`](http://stackoverflow.com/a/5625852/284240). – Tim Schmelter Oct 22 '14 at 13:42
  • @TimSchmelter No coming from C#- but if the Date object shouldn't have a Time component, then why does it have any time component at all, and if it is supposed to, I'm still not seeing why it is not grabbing the Time section supplied (again, **if** supplied- it makes perfect sense to me to default to midnight if nothing is supplied). – Nyra Oct 22 '14 at 13:52
  • 1
    @alykins: every `DateTime` has a time component. It's the same as if you'd say: _give me an hour without minutes_. Every hour can also be represented by 60 minutes. But by using `dt.Date` you say explicitly that you want that `DateTime` "without" time which means midnight and is a shortcut for `new DateTime(dt.Year,dt.Month,dt.Day)`. – Tim Schmelter Oct 22 '14 at 14:00
  • @TimSchmelter That makes a lot of sense- thank you. That is what I was looking for (a direct elaboration)- if this gets re-opened, and you post that as answer I will mark. – Nyra Oct 22 '14 at 14:01

3 Answers3

3

DateTime.Date means the day, the same way DateTime.Today returns the current date's DateTime at midnight (as opposed to DateTime.Now). So what do you expect it to return instead? A DateTime is a struct which always contains a time even if it's set to 0:00:00.

So every DateTime has a time component. It's the same as if you'd say: give me an hour without minutes. Every hour can also be represented by 60 minutes. By using dt.Date you say explicitly that you want that DateTime "without" time which means midnight and is a shortcut for new DateTime(dt.Year,dt.Month,dt.Day).

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

As far as I'm aware, aside from being the start of the date, it's there so you can ignore the Time part of the DateTime.

I'm sure there's better, more detailed explanations (and I'm sure someone, somewhere will come along and mention the use of NodaTime - possibly Jon Skeet himself - and it's Date class)

Joe
  • 1,214
  • 3
  • 15
  • 33
0

Because it's got to be set to something, and midnight (ie all zeros) is as good a value as any!

Seriously though, it makes sense at you've asked for a date, and 00:00:00 is the start of that date. There's no Date type in the framework, the designers overloaded DateTime to cover both, and midnight was chosen as the time in the day.

Sean
  • 60,939
  • 11
  • 97
  • 136