-1

I'm attempting to convert the following string to DateTimeOffset

20201106 13:09:14.510952 +0000

I'm using the following format to attempt to convert it

DateTimeOffset dtofResult = new DateTimeOffset();

DateTimeOffset.TryParseExact("20201106 13:09:14.510952 +0000", @"yyyyMMdd HH\:mm\:ss\FFFFFF zzz\s", CultureInfo.InvariantCulture, DateTimeStyles.None, out dtofResult);

This keeps returning me the default value and not the converted string.

I'm not sure if this is an issue with the format or possibly an issue with culture variants?

Would appreciate any help on this issue!

benbants
  • 590
  • 7
  • 30

1 Answers1

1

There are two things mismatched between your input string and your format string.

The first mismatch is a simple typo. The format string is missing . between ss and FFFFFF. The result is that you have \F for a literal F character, which your input does not have. To fix that, insert . between \ and F.

You don't really need to escape the dot, though, because it's not interpreted as anything special. It doesn't hurt but it's also not adding any value for you. You can remove the backslash.

The second mismatch is the \s at the end. What that's doing is attempting to match a literal s character at the end of the input, which your input also does not have. Remove \s from the end of the format string.

What you were trying to achieve there is unclear. My best guess is that you wanted to ignore trailing whitespace, as it looks like a regex pattern for a whitespace character. If that's what you want, pass DateTimeStyles.AllowTrailingWhite instead of DateTimeStyles.None.

After applying those corrections, the format string becomes:

@"yyyyMMdd HH\:mm\:ss.FFFFFF zzz"
madreflection
  • 4,744
  • 3
  • 19
  • 29