string to DateTime conversion in C#

后端 未结 5 1250
陌清茗
陌清茗 2020-12-20 18:11

Stupid questions but cant get my head around it... I have a string in this format 20081119

And I have a C# method that converts the string to a DateTime to be entere

5条回答
  •  旧巷少年郎
    2020-12-20 18:29

    I'm thinking it's due to the culture set in CurrentCulture, without knowing what that is, I can't be certain, but specifying en-US works on my end. Here is the code I have:

    var dateString = "20081119";
    var enUS = new System.Globalization.CultureInfo("en-US");
    var resultingDate = DateTime.ParseExact(dateString,"yyyyMMdd",enUS);
    Console.WriteLine(resultingDate.ToString());
    

    Give it a try and see if it works for you.

提交回复
热议问题