The format you get is close to the date and time pattern ("o") and universal sortable date and time patterns ("u") for the standard date and time format, as described here .
One tricky solution would be to massage the string suitable for the template, and then use the standard format string āoā or āuā with ParseExact .
The best way would be to create a custom format string that matches the data you are already receiving. In the "How standard format strings work" section of the standard date and time format strings page, you will see all arbitrary format strings equivalent to "o" and "u". This should give you a good start.
EDIT : add code
string format = "yyyyMMddHHmmss.f'Z'"; string target = "20070828085401.0Z"; DateTime d = DateTime.ParseExact(target, format, CultureInfo.InvariantCulture);
In the comments, lixonn notes that using the above format string, ParseExact will not be able to successfully 199412160532-0500 time string like 199412160532-0500 .
He will also not analyze a number of other valid strings, such as times, without the 'Zulu' trailing indicator ( 20070828085401.0 ); time without a fractional part ( 20070828085401Z ) and time representing minutes and seconds as a fractional hour ( 2007082808.90028Z ).
The format string can be made a little more forgiving by replacing the hard-coded 'Z' custom K specifier that will take 'Z', an offset of type -0500 and nothing. Whether this additional flexibility is useful depends on your application.
Please note that even with the K specifier the Lixonn string will not be successfully parsed, since it does not have a fractional part to match the .f component of the format string.
source share