Parsing a Date Like “Wednesday 13th January 2010” with .NET

前端 未结 7 1700
孤城傲影
孤城傲影 2021-01-07 19:49

How can I convert the following strings to a System.DateTime object?

Wednesday 13th January 2010
Thursday 21st January 2010
Wednesday 3rd February 2010

7条回答
  •  执念已碎
    2021-01-07 20:31

    Expanding on Kenny's approach, I added some code to pass integers to the DateTime variable...

    string sDate = "Wednesday 13th January 2010";
    string[] dateSplit = sDate.Split (' ');
    string day = dateSplit[1].Substring(0, dateSplit[1].Length - 2);
    int monthInDigit = DateTime.ParseExact(dateSplit[3], "MMMM", CultureInfo.InvariantCulture).Month;
    DateTime date = new DateTime(Convert.ToInt16(year), monthInDigit, day);
    

提交回复
热议问题