DateTime.ParseExact ignore first char C#

牧云@^-^@ 提交于 2019-12-02 02:33:21

问题


I get a string value from a device like "1140421164500". I have to convert it to DateTime type. I want to use DateTime.ParseExact function. I know that I can convert it by omitting the first char manually like the following:

DateTime.ParseExact("140421164500", "yyMMddHHmmss", CultureInfo.InvariantCulture);

But I want to avoid omitting the first char manually. I want to ignore it with a wildcard char in ParseExact function like:

DateTime.ParseExact("1140421164500", "*yyMMddHHmmss", CultureInfo.InvariantCulture);

Let me note that the first char can be 1 for daylight saving time is active, or 0 for passive. The device can send me also like "0140101000000".

Is there anything like that for this function?


回答1:


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.



来源:https://stackoverflow.com/questions/23198922/datetime-parseexact-ignore-first-char-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!