Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否将常规SQL时间格式转换为c#(所有变体)?_C#_.net_Datetime_Datetime Format_Datetime Parsing - Fatal编程技术网

是否将常规SQL时间格式转换为c#(所有变体)?

是否将常规SQL时间格式转换为c#(所有变体)?,c#,.net,datetime,datetime-format,datetime-parsing,C#,.net,Datetime,Datetime Format,Datetime Parsing,如果我有这些c#字符串中的常规sql有效日期: (每个日期可以按不同的顺序排列)例如: 我想将字符串日期转换为DateTime(c#)。(我希望能够转换上述列表中的每种格式) 而解析精确需要我3组合,有更好的解决方案吗?我想我找到了。。。 string[]str=new[]{“2010年2月1日”、“2010年2月1日”、“2010年2月1日”、“2010年2月1日”、“2010年2月1日”、“2010年2月1日”}; Thread.CurrentThread.CurrentCulture=C

如果我有这些c#字符串中的常规sql有效日期:

(每个日期可以按不同的顺序排列)例如:

我想将字符串日期转换为
DateTime
(c#)。(我希望能够转换上述列表中的每种格式)

而解析精确需要我
3组合,有更好的解决方案吗?

我想我找到了。。。

string[]str=new[]{“2010年2月1日”、“2010年2月1日”、“2010年2月1日”、“2010年2月1日”、“2010年2月1日”、“2010年2月1日”};
Thread.CurrentThread.CurrentCulture=CultureInfo.CreateSpecificCulture(“en-US”);
CultureInfo culture=CultureInfo.CreateSpecificCulture(“en-US”);
对于(int i=0;i
3!只有6。。。然而,您可以在空间上拆分,并确定每个块是字母、4位还是2位。。。顺便说一句,这和SQL有什么关系?是的,我知道,这就是我想要做的。。。但在我继续之前,只需检查其他解决方案是否为年4位,月3位,日2位。你可以使用一个带有匹配组的正则表达式,并进行一次精确的解析。确定你不会得到“Feb”作为02吗?那么你就死定了。@MarcGravelSQL将永远知道如何解析这种格式。无论每个参数的位置如何。datepicker将值以这种格式存储在文本框中,然后以c#格式存储-我需要将其转换为c#datetime。无需设置(污染)CurrentThread.CurrentCulture
<代码>解析(…,区域性)应该足够了。@HenkHolterman yep…虽然当我显示时它确实改变了顺序(我的计算机在dd/MM/yyyy),所以它显示:01/02/2010…但只是为了显示(这与此无关)@abatishchev它类似的控制台。Linqpad中的Writeline。(很棒的工具-你会喜欢的)
01 feb 2010
feb 01 2010
2010 01 feb
...
...
string[] str = new[] { "01 feb 2010","feb 01 2010","2010 01 feb","2010 feb 01","feb 2010 01" };
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");                       
for (int i = 0; i < str.Length; i++)
{
    DateTime t = DateTime.Parse(str[i], culture);
    t.Dump();
}