C # - Analysis of partial dates in different formats

For use in the search method, I would like to analyze partial dates in different formats. I want the user to be able to search for day and month, month and year, as well as day and month and year.

But all the more problematic is that I want to do this in all possible date formats, depending on the country in which the user is located. This is an ASP.NET page, and I can use CultureInfo and therefore CultureInfo.DateTimeFormat.

Do you have any idea how to do this? I think it would be great that a British person could search for 16/05, and an American could enter 05/16, or German could use 16.05 to find the result from May 16th.

I think you can use DateTimeFormat.ShortDatePattern. But how can I use it to get day, month and year out of them?

I think about using type

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

Thanks to everyone who can help me.

+3
source share
2 answers

Like most of you mentioned, I think that I will choose a method with a dumper or a drop-down list. But for those who are interested in another solution:

I thought about different formats for a while and found that they are very similar in most cases. So I wrote a method that returns a better matching version of more commonly used patterns. So 'dmy' → 'dmy' or 'd / my' → '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 number of possible formats from 26 to these 8:

  • d / m / g
  • dmy
  • dt
  • //
  • y.m.d
  • --
  • //
  • m.d.y

3 , [/.-] .

P.S. . .

+1

, ( ) . - :

" (DD-MM): _ - _"

" (MM-DD): _ - _"

....

0

Source: https://habr.com/ru/post/1752622/


All Articles