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.

Marks
  • 3,451
  • 5
  • 29
  • 44
  • 1
    I think it would be a better idea to have your users select a date from some date picker control. – Ronald Wildenberg Jul 01 '10 at 12:08
  • 1
    Your problem is bigger. If the input is 2/2, is that '2 feb' or 'feb 2002' ? The shortdate format won't be able to help out. – Henk Holterman Jul 01 '10 at 12:15
  • It is not clear whether you know the user's preferred date format or you want to infer it from the input string. If you do know the user's nationality(or preferrences) then you know what those numbers mean, you just have to parse them. Inferring the format from input string is a little more complicated... – Andy Jul 01 '10 at 12:29
  • @Henk: since there is no datetime format which has the year as single digit, your example would clearly be '2 feb'. But problem still exists, because there are formats that have 2 digits years, where '10/11' could be '10 nov' or 'sep 2011' – Marks Jul 01 '10 at 12:36
  • Alternatively you can give users three drop down to select from... You can also have a default blank lines in all the three. In case user wants to search for Jan 2010, he could select blank from the day drop down... – The King Jul 01 '10 at 12:42
  • 1
    @Ronald Wildenberg, a DateTimePicker won't ley you choose a _partial_ date. – Henk Holterman Jul 01 '10 at 14:09

2 Answers2

2

Like most of you mentioned, i think i will choose a method with a datepicker or dropdowns. But for those who are interested in another solution:

I thought about the different formats for a while and found that they are very similar in most cases. So i wrote a method that returns the best matching version of more often used patterns. So 'd.m.y.' -> 'd.m.y' or '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();
}

This reduces the amount of possible formats from 26 to these 8:

  • d/m/y
  • d.m.y
  • d-m-y
  • y/m/d
  • y.m.d
  • y-m-d
  • m/d/y
  • m.d.y

These are only 3 different formats for parsing via regex if you use [/.-] for the seperators.

P.S. Still doesn't fix the problem with partial dates. But search with full date for all cultures is possible.

Marks
  • 3,451
  • 5
  • 29
  • 44
0

You can define a number of input formats and then impose one of them(based on info about the user) in your forms. Something like:

"Enter date(DD-MM):_ - _" for british

"Enter date(MM-DD):_ - _" for american

etc...

Andy
  • 3,349
  • 2
  • 20
  • 29