Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strategy for Incomplete Dates

Working on an application where we would like the user to be able to enter incomplete dates.

In some cases there will only be a year - say 1854, or there might be a year and a month, for example March 1983, or there may be a complete date - 11 June 2001.

We'd like a single 'date' attribute/column - and to be able to sort on date.

Any suggestions?

like image 594
Blue Waters Avatar asked Nov 29 '10 05:11

Blue Waters


2 Answers

Store the date as an integer -- yyyymmdd.

You can then zero out any month or day component that has not been entered

Year only: 1954 => 19540000
Year & Month: April 2004 => 20040400
January 1st, 2011 => 20110101

Of course I am assuming that you do not need to store any time of day information.

You could then create a struct to encapsulate this logic with useful properties indicating which level of granularity has been set, the relevant System.DateTime, etc

Edit: sorting should then work nicely as well

like image 187
cordialgerm Avatar answered Oct 21 '22 11:10

cordialgerm


I can't think of a good way of using a single date field.

A problem you would get if you used January as the default month and 1 as the default day like others have suggested is, what happens when they actually pick January? How would you track if it's a selected January or a defaulted January.

I think you're going to have to store a mask along with the date. You would only need a bit per part of the date, which would only be 6 bits of data.

M|D|Y|H|Min|S

Month Only 1|0|0|0|0|0 = 32

Year Only 0|0|1|0|0|0 = 8

Month+Year 1|0|1|0|0|0 = 40

AllButMinSec 1|1|1|1|0|0 = 60

You could put this into a Flag Enum to make it easier to use in code.

like image 1
jamiegs Avatar answered Oct 21 '22 13:10

jamiegs