C# .NET 中的模糊日期时间选择器控件?
我正在 C# 中为 winforms 应用程序实现模糊日期控件。 模糊日期应该能够采用模糊值,例如
- 去年六月
- 2 小时前
- 2 个月前
- 上周
- 昨天
- 去年
等
是否有“模糊”日期时间选择器的示例实现?
任何实现这种控制的想法将不胜感激
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
解析非常简单。 它可以作为一堆正则表达式和一些日期计算来实现。
下面的示例可以轻松扩展以满足您的需求。
我粗略地测试过它,它至少适用于以下字符串:
辅助类:
用法示例:
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:
我们的用户使用的系统之一允许他们输入如下日期:
他们似乎喜欢它,并在我们的应用程序中请求它,所以我想出了以下代码。 ParseDateToString 将采用上述形式之一的字符串,加上其他一些形式,计算日期,并以“MM/DD/YYYY”格式返回。 可以很容易地更改它以返回实际的 DateTime 对象,以及添加对小时、分钟、秒或任何您想要的内容的支持。
列表中唯一可能比较困难的示例是“Last June”,但您可以通过计算自去年 6 月以来已经过去了多少个月来计算要传入的字符串。
当然,这取决于 DateTime 的 AddMonths 函数的准确性,而且我还没有真正测试过边缘情况。 它应该为您提供去年六月的日期时间,您可以使用它来查找该月的第一天和最后一天。
其他一切都应该很容易使用正则表达式进行映射或解析。 例如:
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:
我们有类似的控制。 我们只是添加一个组合框列表 - 控件来选择您的选择。
周期选择器:
只需选择对您的目的有意义的选择即可。
实现这个比解析文本要容易得多。 计算相当简单。
请务必注意您选择的是句号。 去年是指从2008年1月开始> 2008 年 12 月。两个小时前,从现在到现在 - 2 小时。 ETC。
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.
Piotr Czapla 的答案中有一个错误:
使用 AddMonths 而不是 AddHours()。
PS:由于论坛积分低,我无法评论他的回答。 我已经浪费了时间来调试它,为什么当我尝试“5小时前”时它会删除5天。
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".