简体   繁体   English

DateTime.TryParse适用于任何文化

[英]DateTime.TryParse for any culture

There are lots of thread for this , but I am still blocked at the following: 为此有很多线程,但是我仍然在以下方面受阻:

What I have: 我有的:

I am trying to build windows phone App which will pick Date of Birth of an individual 我正在尝试构建Windows Phone应用,它将选择一个人的出生日期

Code Behind: 背后的代码:

string dateString = "";

  DateTime dt = DateTime.Now;
     if (value != null && DateTime.TryParse(value.ToString(),  culture, DateTimeStyles.None, out dt))
  {
    if (dt.Equals(DateTime.MinValue))
    {
      //dateString = "mm/dd/yyyy";
      return "";
    }
    else
      return dt.ToShortDateString();
  }
  else
    return dateString;
}

what I need: 我需要的:

I want it to parse any date format which should be culture Independent. 我希望它解析任何与文化无关的日期格式。

What I tried: 我试过的

1.I tried using CultureInfo.InvariantCultute, CultureInfo.CurrentCulture 1.我尝试使用CultureInfo.InvariantCultute,CultureInfo.CurrentCulture

2.Tried using ExactParser as follows: 2.使用ExactParser尝试如下:

string[] formats = {"M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt", "d/M/yyyy hh:mm:ss tt", "dd/MM/yyyy hh:mm:ss tt", "d/M/yyyy" , "dd/MM/yyyy" , "M/d/yyyy" ,"MM/dd/yyyy", 
               "MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss",   "dd/MM/yyyy hh:mm:ss", "d/M/yyyy h:mm:ss",
               "M/d/yyyy hh:mm tt", "M/d/yyyy hh tt",  "d/M/yyyy hh:mm tt", "d/M/yyyy hh tt",
               "M/d/yyyy h:mm", "M/d/yyyy h:mm", "d/M/yyyy h:mm", "d/M/yyyy h:mm",
               "MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm","dd/MM/yyyy hh:mm", "dd/M/yyyy hh:mm"};


 if (value != null && DateTime.TryParseExact(value.ToString(), format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))

but that is too hardcoded and will not cover several cases. 但这太硬了,无法涵盖几种情况。

Is there any way to pick DOB in any format? 有什么方法可以选择任何格式的DOB?

Any help would be Appreciated. 任何帮助,将不胜感激。 Thanks!! 谢谢!!

Not really, because everyone has their own date formatting. 并非如此,因为每个人都有自己的日期格式。

另外,这个!

What you're basically trying to do is magically deduce the dates from the second half of the above XKCD comic . 您基本上想做的是从上述XKCD 漫画的后半部分神奇地推断出日期。 ;) ;)

That said, the only way you could TRY and do this, is to parse a string with every format you think it might be, and then make a sanity check in every case where a parse was successful. 也就是说,您可以尝试并执行此操作的唯一方法是,以您认为可能的每种格式解析一个字符串,然后在解析成功的每种情况下进行完整性检查。 Said sanity check would be difficult however... Does 11-02 mean February 11th or November 2nd? 但是,要进行上述健全性检查会很困难... 11-02是2月11日还是11月2日? You'd require some sort of context. 您需要某种上下文。

Sorry, but it seems impossible, since datetime could be ambiguous , and that's why misinterpreted eg 抱歉,但这似乎是不可能的,因为日期时间可能不明确 ,这就是为什么会误解例如

"01/02/03" is "01/02/03"

01 Feb 2003 (Russia)

02 Jan 2003 (USA)

03 Feb 2001 (China)

see http://en.wikipedia.org/wiki/Date_format_by_country 参见http://en.wikipedia.org/wiki/Date_format_by_country

This will try all culture: 这将尝试所有文化:

public static bool TryParseAnyDate(string dateValue, out DateTime result)
{
  result = DateTime.MinValue;
  foreach (CultureInfo cultureInfo in CultureInfo.GetCultures(CultureTypes.AllCultures))
  {
    if (DateTime.TryParse(dateValue, cultureInfo, DateTimeStyles.None, out result))
    {
      return true;
    }
  }
  return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM