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

前端 未结 7 1695
孤城傲影
孤城傲影 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:13

    What about strip them?

    string value = "Wednesday 13th January 2010";
    DateTime dt;
    DateTime.TryParseExact(
        Regex.Replace(value, @"(\w+ \d+)\w+ (\w+ \d+)", "$1 $2"),
        "ffffdd d MMMM yyyy", 
        DateTimeFormatInfo.InvariantInfo, 
        DateTimeStyles.None, out dt);
    

提交回复
热议问题