user1613797
user1613797

Reputation: 1267

Which C# datatype should be used to store dates without time?

There is a data type for storing dates without time in PostgreSQL. What is the best data type for storing dates without time in C#?

We know, DateTime can be used with 00:00:00 time, but this is very misleading for external users and dates also doesn't have time zones, but DateTime does. NodaTime cannot be used in our project, so we are looking for a native .NET solution.

Upvotes: 3

Views: 3082

Answers (2)

Panagiotis Kanavos
Panagiotis Kanavos

Reputation: 131631

DateTime doesn't have timezone info, only a generic UTC/"Local" indicator.

There is no date- or time-only type in .NET at the time. While the lack of Date is an inconvenience, the lack of TimeOfDay is a bigger problem. People are experimenting with Date and TimeOfDay types though at the CoreFX Lab repository.

At the moment you'll have to use the DateTime class or create your own Date class. Even the SQL Server ADO.NET provider doesn provide a date or time only class.

Of course you can always "borrow" the MIT-licensed experimental Date implementation from CoreFX Lab.

NOTE

I understand the pain and the need as I work on air travel related software. Having to work with timezones when I only want to compare against a date is really tiresome. Things begin to really ---- though when I need to convert between Json and DateTime to pass date only values.

Upvotes: 5

Christos
Christos

Reputation: 53958

There isn't any data type that would suit your needs in .NET.

There is only the DateTime struct that holds a value for a date and time.

Furthermore, I don't see the reason why don't you use the DateTime and show only the date to the users.

Upvotes: 1

Related Questions