3

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.

user1613797
  • 1,197
  • 3
  • 18
  • 33
  • How will your "external users" read the dates? I'd say use ISO 8601 format without time. So use DateTime internally, and print them as `YYYY-MM-DD`. – CodeCaster Sep 02 '15 at 14:06
  • 1
    @CodeCaster this isn't a display only issue. Comparing dates for example should *not* force you to convert everything to the same timezone. This is a *real* pain in travel-related applications. Equally, comparing times should *not* force you to use some arbitrary date as base. – Panagiotis Kanavos Sep 02 '15 at 14:15
  • @PanagiotisKanavos I know very well about the timezone and time problems of the BCL's `DateTime` structure, but I don't see OP talking about timezones. While a lot of software does now or in the future need to deal with timezones, if OP's dates are all in the same timezone and always will be, using DateTime with a time part of "00:00" won't harm anyone. The only problem OP is having is with the displaying/exporting part, to which I suggested a solution with a question - which OP did not answer. – CodeCaster Sep 02 '15 at 14:17
  • @CodeCaster the OP does mention timezones and they *do* affect conversions and comparisons - unless *all* dates are UTC, even those eg retrieved from input parameters in an MVC action or retrieved from `date` fields in SQL Server, there will be unexpected timezone conversions when you compare values with different DateTimeKind. – Panagiotis Kanavos Sep 02 '15 at 14:26
  • 1
    @PanagiotisKanavos I referred to _"doesn't have time zones"_, i.e. all dates are in the same timezone. Of course a warning is in place, but given the constraints it seems like you're addressing a non-issue and `System.DateTime` is fine here. When comparing DateTimes of different kinds, you [don't need to convert them](http://stackoverflow.com/questions/6930489/). Yes, using DateTime has its caveats, but it's not as unusable as you seem to be claiming. If all OP wants is to output a date, `DateTime.ToString("YYYY-MM-DD")` _can_ be a viable solution. – CodeCaster Sep 02 '15 at 14:30
  • Late to the party but I agree with CodeCaster. If you are simply representing a Date and not doing any time-zone manipulation, DateTime is fine and should be the recommended solution. – Cory Sep 02 '15 at 16:24

2 Answers2

5

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.

Sedat Kapanoglu
  • 46,641
  • 25
  • 114
  • 148
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
1

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.

Christos
  • 53,228
  • 8
  • 76
  • 108
  • Microsoft does this as well with their SQL Server implementation: https://msdn.microsoft.com/en-us/library/cc716729(v=vs.110).aspx – Caramiriel Sep 02 '15 at 14:07
  • Using DateTime is *not* optimal for date- or time-only values. Travel dates for example should *not* have to be converted to UTC to compare. There are many situations where one has to perform timezone conversions just to compare two dates – Panagiotis Kanavos Sep 02 '15 at 14:17
  • @PanagiotisKanavos Sure thing but I just wanted to pointed out that this is the situation in .NET, unless your resort to the usage of a library like NodaTime, where comparisons (and generally) are far more easier. Personally, I had the to face with a problem of converting one DateTime to another DateTime in another TimeZone. For my goodness , I knew that the DateTime was in UTC and the things gone a bit easier, using NodaTime. When I was reading the DateTime from the DB, it's Kind was unspecified and I had first to create a DateTime with a UTC specified Kind...before I proceeded with the rest. – Christos Sep 02 '15 at 20:44
  • @PanagiotisKanavos Generally, DateTime's are a bit hard to be manipulated as they had be designed in .NET. Fortunately, NodaTime address this issue. Ευχαριστώ (Thanks) for your comment. – Christos Sep 02 '15 at 20:49