How to parse a string to DateTime with “yyyyMMdd Hmm” format?

后端 未结 2 857
一个人的身影
一个人的身影 2020-12-12 01:14

I\'m having huge problems with solving this problem. I\'m trying to parse a string using Datetime.ParseExact().

I have the following code:

DateTime.P         


        
2条回答
  •  隐瞒了意图╮
    2020-12-12 01:40

    Just insert a separator before minutes (for example, a whitespace) and then you can parse it like this:

    string example = "20151210 832";
    example = example.Insert(example.Length - 2, " ");
    var dateTime = DateTime.ParseExact(example, "yyyyMMdd H mm", CultureInfo.InvariantCulture);
    

    I assume that the datetime string always contains two digits specifying minutes (check an article about Custom Date and Time Format Strings). If my assumption is wrong then this string cannot be parsed.

提交回复
热议问题