One of the first things I learned about dates and times in c# (and various other languages) is that a date is stored as a DateTime with the time component set to midnight. There is no difference between "Jan 1, 2017" and "January 1, 2017 00:00:00.000". If you need to display just a date, you can use formatting functions like ToString
to remove the time portion.
This seems to cause a lot of problems, a few of which I shall list here:
DateTime takes up more storage space than needed: 8 bytes instead of the 3 required for a simple date.
Use of 00:00:00 for "no date" conflates "a known value of 0" with "unknown value." This seems rather noobish, in the same way a new programmer might confuse a null string with an empty string. Null and empty are different, and "no time" and "midnight" are different ideas.
You see idioms like this one poppping up, even in relatively simple comparisions:
if (startDate >= myDateTime && myDateTime < endDate.AddDays(1))
or
if (startDateTime >= myDate && myDate <= endDateTime.Date)
instead of a far simpler
if (startDate >= myDate && myDate <= endDate)
When system components located in different time zones call each other over web services, and the schema for those web services is generated from c# types, a time element can be introduced (so that the resulting date occurs early in the morning), or the date value actually may shift by a day (e.g. the final date is actually the prior day at a time late in the evening). This can make it painful to transmit values that are agnostic with respect to timezone, e.g. a date of birth, or a credit card expiration date.
SQL Server has a Date
type that stores only month, day, and year.
XSD specifies different data types for Date, Time, and DateTime.
Why doesn't c# have such a type?
How do you work around it? For example, can c# properties be attributed so that they will serialize as a Date instead of a DateTime?
System.Int32
=int
)?" It does not answer the question of why there is not a separate data type just for Date.