2011-12-07 42 views
0

我的问题是我无法将美国日期值转换为英国格式。 TryParse条件总是作为false出现,因此outputValue永远不会被填充。将美国日期时间转换为文化特定值

执行有问题吗?请指出或建议更好的选择。谢谢。

string filterValue = "12/22/2011" 
    string outputValue = "" 
    DateTime dt;      
    string strCulture = "en-GB"; 
    CultureInfo ci = new CultureInfo(strCulture);      

    if (DateTime.TryParse(filterValue, ci, DateTimeStyles.AdjustToUniversal, out dt)) 
     outputValue = dt.ToString("d", ci);      

回答

1

您使用英国文化来解析美国格式的日期。使用下面的代码。

string filterValue = "12/22/2011"; 
DateTime dt; 
string outputValue = ""; 

if (DateTime.TryParse(filterValue, CultureInfo.GetCultureInfo("en-US"), DateTimeStyles.AdjustToUniversal, out dt)) 
{ 
    outputValue = dt.ToString("d", CultureInfo.GetCultureInfo("en-GB")); 
} 
相关问题