-2

I receive the datetime string below from a web service and try to transform it into a UTC datetime object but an exception is thrown.

try
{
    string ReceivedDateTimeString = "2012-10-09T07:42:13.409191Z";
    DateTime TransformedReceivedDateTimeString = DateTime.ParseExact(ReceivedDateTimeString, "o", CultureInfo.InvariantCulture, DateTimeStyles.None);
}
catch (Exception e)
}

Why is an exception thrown? Is there something wrong with the format of the string or with my transformation?

Peter Olson
  • 139,199
  • 49
  • 202
  • 242
cs0815
  • 16,751
  • 45
  • 136
  • 299
  • http://stackoverflow.com/questions/12705874/utc-string-to-datetime-exception - sorry can't use same question title – cs0815 Oct 09 '12 at 08:18
  • 1
    IMHO the downvotes are a bit harsh. Neither of the linked questions gives an answer that clearly applies to this scenario. – Joe Oct 09 '12 at 08:53
  • Thanks Joe - that's why I created a new question. – cs0815 Oct 09 '12 at 10:32
  • You still have only 6 decimal places, that part of Jon Skeet's answer still applies. – Kevin Oct 17 '12 at 03:39

3 Answers3

2

Try this:

string ReceivedDateTimeString = "2012-10-09T07:42:13.409191Z";
DateTime TransformedReceivedDateTimeString = 
    DateTime.ParseExact(ReceivedDateTimeString, 
                       "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'FFFFFFFK", 
                       CultureInfo.InvariantCulture, 
                       DateTimeStyles.RoundTripKind);

The documentation for the "o" format specifier states that:

The "O" or "o" standard format specifier corresponds to the "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffK" custom format string for DateTime values and to the "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffzzz" custom format string for DateTimeOffset values.

I.e. 7 decimals, not 6 as in your string.

The formatted string can be parsed back by using the DateTimeParse(String, IFormatProvider, DateTimeStyles) or DateTimeParseExact method if the styles parameter is set to DateTimeStyles.RoundtripKind.

I.e. you need to specify DateTimeStyles.RoundtripKind.

In the above, I've used "FFF..." rather than "fff..." as this will accept a variable number of decimals (missing trailing decimals are treated as zero). This is to conform to the principle of being "tolerant on input, strict on output".

Also I've specified DateTimeStyles.RoundtripKind to preserve the Kind property when parsing (Utc in this case). If you don't do this, the result will be converted to local time.

Joe
  • 122,218
  • 32
  • 205
  • 338
1

What about this:

DateTime TransformedReceivedDateTimeString = DateTime.Parse(ReceivedDateTimeString).ToUniversalTime();
Danilo Vulović
  • 2,983
  • 20
  • 31
1

Well, you were given the correct answer the last time by Jon Skeet: UTC string to DateTime exception

Note that there are 7 fs there, but you've only got 6 decimal places.

It's the same error now. You've got three possible solutions:

  1. As you stated on your previous question (comment), you could have your data provider send you the data on the standard format.
  2. If you don't have access to the data, you could change the style parameter to a custom format: "yyyy-MM-ddTHH:mm:ss.ffffffK"
  3. Or, you could add another decimal at the end to follow the standard format: "2012-10-09T07:42:13.4091910Z";

Change the standard format

Community
  • 1
  • 1
Niklas
  • 13,005
  • 23
  • 79
  • 119
  • 1
    If I'm trying to parse input that presumably comes from an external source, it's not very helpful to say "change the input". – Joe Oct 09 '12 at 08:54