DateTime.ParseExact ignore first char C#

前端 未结 1 833
独厮守ぢ
独厮守ぢ 2021-01-22 08:32

I get a string value from a device like \"1140421164500\". I have to convert it to DateTime type. I want to use DateTime.ParseExact<

相关标签:
1条回答
  • 2021-01-22 09:00

    There is no wildcard character in custom date and time format that it can parse every possible character in your string.

    You can add your format the first character of your string like;

    string s = "1140421164500";
    Console.WriteLine(DateTime.ParseExact(s, s[0] + "yyMMddHHmmss", 
                      CultureInfo.InvariantCulture));
    

    Output will be;

    4/21/2014 4:45:00 PM
    

    Here a demonstration.

    0 讨论(0)
提交回复
热议问题