2011-12-07 45 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")); 
} 
相關問題