将22.08.2010转换为2010年8月22日

Dor*_*eka 2 c# asp.net

大家好我所有的日期为22.08.2010 in到一个字符串我怎么能把它转换为22/08/2010

jet*_*hro 10

string strDate = "22.08.2010"
string result = strDate.Replace('.','/');
Run Code Online (Sandbox Code Playgroud)

更一般的解决方案

DateTime time = DateTime.ParseExact(strDate,"dd.MM.yyyy",CultureInfo.InvariantCulture);
time.ToString("dd/MM/yyyy");
Run Code Online (Sandbox Code Playgroud)

  • 建议使用`DateTime.TryParseExact` (2认同)

Dar*_*rov 5

您可以尝试使用ParseExact方法

string result = DateTime.ParseExact(
    "22.08.2010", 
    "dd.MM.yyyy", 
    CultureInfo.InvariantCulture
).ToString("dd/MM/yyyy");
Run Code Online (Sandbox Code Playgroud)

如果输入格式不能保证是有效日期,则可以尝试使用TryParseExact方法.