I am implementing a Fuzzy Date control in C# for a winforms application. The Fuzzy Date should be able to take fuzzy values like
- Last June
- 2 Hours ago
- 2 Months ago
- Last week
- Yesterday
- Last year
and the like
Are there any sample implementations of "Fuzzy" Date Time Pickers?
Any ideas to implement such a control would be appreciated
PS: I am aware of the fuzzy date algorithm spoken about here and here, I am really looking for any ideas and inspirations for developing such a control
One of the systems our users use allows them to enter dates like so:
They seem to like it, and requested it in our app, so I came up with the following code. ParseDateToString will take a string of one of the forms above, plus a few others, calculate the date, and return it in "MM/DD/YYYY" format. It's easy enough to change it to return the actual DateTime object, as well as to add support for hours, minutes, seconds, or whatever you want.
The only example in your list that might be difficult would be "Last June", but you could just calculate the string to pass in by figuring out how many months it's been since last June.
Of course, that'll depend on the accuracy of DateTime's AddMonths function, and I haven't really tested edge cases for that. It should give you a DateTime last June, and you could just use that to find the first and last of the month.
Everything else should be fairly easy to map or parse with regular expressions. For example:
The parsing is quite easy. It can be implemented as bunch of regexps and some date calculations.
The sample below can be easily extended to suit your needs. I've roughly tested it and it works at least for the following strings:
The helper class:
Usage example:
We have a similar control. We just add a list of combo boxes - controls to pick your choice.
PeriodSelector:
And just take the choices that make sense for your purpose.
It's a lot easier to implement this then parsing the text. The calculations are rather straightforward.
It's important to see that you are picking a period. Last year means from january 2008 > december 2008. Two hours ago from now untill now - 2 hours. Etc.
There is a bug in Piotr Czapla's answer:
AddMonths is used instead of AddHours().
PS: I can't comment on his answer because of low forum points. I've already wasted time on debugging it of why it removes 5 days when I try with "5 hours ago".