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

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

    Where does "th", "st","nd" or "rd" appear below?

    • monday
    • tuesday
    • wednesday
    • thursday
    • friday
    • saturday
    • sunday

    • january

    • february
    • march
    • april
    • may
    • june
    • july
    • august
    • september
    • october
    • november
    • december

    However you know those 4 will always be followed by a space. So unless I've missed something, a simple

    value = value.Replace("August","Augus").Replace("nd ","").Replace("st ","").Replace("nd ","").Replace("rd ","").Replace("Augus","August");
    DateTime dt;
    DateTime.TryParseExact(value,"DDDD dd MMMM yyyy", DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out dt);
    

提交回复
热议问题