3

For use in a search method i would like to parse partial dates in different formats. I want to enable the user to search for day and month or month and year or also day and month and year.

But the more problematic thing is that i want do this in all possible date formats depending on the country, the user is in. It's a ASP.NET page and i can use the CultureInfo and therefore also the CultureInfo.DateTimeFormat.

Do you have any idea, how to do something like that? I think it would be great that a british person could search 16/05, while an american could enter 05/16 or a german could use 16.05 to find a result from the 16th of may.

I think it could be possible to use the DateTimeFormat.ShortDatePattern. But how could i use it to get day, month and year out of them?

I think of a use like

public void GetDateParts(string SearchStr, out int? day, out int? month, out int? year)

Thanks to anyone who can help me with this.

4

2 回答 2

2

就像你们提到的大多数人一样,我想我会选择一种带有日期选择器或下拉菜单的方法。但对于那些对另一种解决方案感兴趣的人:

我考虑了一段时间的不同格式,发现它们在大多数情况下非常相似。所以我写了一个方法来返回更常用模式的最佳匹配版本。所以'dmy' -> 'dmy' 或'd/m y' -> 'd/m/y'。

public static string SimplifyDateFormat(DateTimeFormatInfo DTFI)
{
    StringBuilder SB = new StringBuilder(DTFI.ShortDatePattern.ToLower());

    SB.Replace("m y", "m/y").Replace(" 'г.'", "").Replace(" ", "").Replace("dd", "d")
    .Replace("mm", "m").Replace("yyyy", "y").Replace("yy", "y");

    if (SB[SB.Length - 1] == '.') SB.Remove(SB.Length - 1, 1);
    return SB.ToString();
}

这将可能的格式数量从 26 种减少到以下 8 种:

  • 日/月/年
  • dmy
  • dmy
  • 年/月/日
  • yd
  • yd
  • 月/日/年
  • 米迪

如果您使用 [/.-] 作为分隔符,这些只是通过正则表达式解析的 3 种不同格式。

PS 仍然不能解决部分日期的问题。但是可以搜索所有文化的完整日期。

于 2010-07-01T14:29:18.117 回答
0

您可以定义多种输入格式,然后在表单中强加其中一种(基于用户信息)。就像是:

“输入日期(DD-MM):_ - _”代表英国

“输入日期(MM-DD):_ - _”对于美国人

ETC...

于 2010-07-01T12:36:46.780 回答