views:

486

answers:

5

You system allows new users to register and enter their wedding date. Being a purist, I wanted to store this as a date. After user interviews, it was clear that the reality differs from this approach. Users are often not sure of the day or the month, and wish to store partial dates, so "June 2009" or "sometime in 2012".

Does anyone know a good design pattern that allows the storing partial dates, and then trying to create a timeline for the big day - even though there are partial dates entered?

+2  A: 

You can always store the date in separate columns:

  • year
  • month
  • day-of-month

Keep them all nullable. You can add a column where you store the computed full time. For that computed time you use some default values for all the columns that are null.

tehvan
Cool - now how do I utalise these columns to find out how many months before their wedding (for example) without having to reinvent the wheel when performing date manipulation?
digiguru
once you have the computed date, you can use the date/time functions of the database in question to do such calculations (done a bit differently in different databases)
tehvan
+3  A: 

You could store it as a range with an after-date and before-date column.

Kristof Neirynck
Interesting, that way I can take an average date and perform date manipulation on it, and this can be dynamic, so as the day approaches, they can decide to lock it down more.
digiguru
For planning or notification (which is what this sounds like aiming at) I would think that the start of the range would be as important or more important than the mid point. For instance "You need to book the singers by the end of them month to get them during January" even if the range is Jan-Mar.
Bell
+2  A: 

Maybe a "fuzzy" date. If they say "June 2009", that could be stored as 15 June 2009, with a variance of +/- 15 days.

"sometime in 2010" would be 01 July 2010 with a variance of 180 days.

So store a date and a # days.

If/how that would work in practice I don't know. Just an idea. A date range might be easier to implement.

MikeW
This is also a very good suggestion, as we have a single date to manipulate, and a grey area that can descend as the date approaches. Interestingly, we could also weight static dates towards the weekends when people tend to get married...
digiguru
+1  A: 

You could store dates as YYYYMMDD, with unknown months/days set to 0. e.g., 20090200 would be "some day in feb 2009". This will all you to sort and compare relatively easily, and if you need to convert it to a date, you can just add 1 to the month or day to get an approximation for more advanced date comparisions.

Richard Levasseur
+1  A: 

I had a similar problem with dates; with the added issue that some people also wanted to say "15 June", without specifying the year. Is this a case you have to handle too?

I stored the day, month and year in three separate columns in the database, with each nullable, then (somewhat) re-invented the wheel in my application (as well as disallowing some cases, such as people saying "The 15th of 1974").

However, as I put all the date-handling logic in my application, I never accessed the three columns individually. With hindsight I wished I had put them in a single column, holding partial dates as strings (something like "19740615", "1974----", "----0615", etc.), just so that I'd have less column clutter.

I guess it depends how much you want to do in the database and how much you want to do in your application. Either way, I think you'll be inventing a slightly differently-shaped wheel. But of course, you're good enough to make some tidy, encapsulated, reusable classes - that'll make it fun! :-)

teedyay
Thanks for the benefit of actually experiencing this before, I see you've had some interesting experience in the matter.
digiguru