I’m doing some domain modelling and coming across a property which to my mind would be best exposed as a Date rather than a DateTime, without a time component.
Is there a good reason why there is no such type in the Framework? It was deemed a good enough idea to add a Date type to SQL Server. Also, if someone knows of a handy implementation of a Date class, please let me know!
2018 edit: I now tackle this using a Value Object Type:
public class Date : ValueOf<DateTime, Date> //Install-Package ValueOf
{
protected override void Validate()
{
if (Value.Date != Value) throw new ArgumentException();
}
}
...
var date = Date.From(someDateTime);
There wasn’t one at the time the question was asked, and yes, there should be one. Fortunately, as of .NET 6 we have
DateOnlyandTimeOnly.I don’t believe there was a good reason other than that the date and time API in .NET isn’t very good in my (very biased) view.
The Noda Time project of which I’m the primary maintainer has
LocalDateandLocalTime, as part of a rather larger set of types.