1

I've been told I'm going to receive a datetime in the format:

yyyy-mm-ddThh:mm:sss

That comes from a Java web service, even if I'm trying to get what's the point for the three "sss" at the end it seems to be the final format and not a typo.

So I'm trying to Parse it using .Net (actually C#)

var s = "2014-09-16T12:17:057";
var d = DateTime.ParseExact(s, "yyyy-MM-ddThh:mm:sss", CultureInfo.InvariantCulture);

But it raises an exception

Sting was not recognized as a valid DateTime

Thanks,

UPDATE:

As suspected, third time the charm and I've been confirmed the actual format should be

yyyy-MM-ddTHH:mm:ss
mitomed
  • 2,006
  • 2
  • 29
  • 58
  • 1
    What web service are you trying to reach? They probably explain how to use it somewhere. – Nzall Sep 16 '14 at 14:51
  • @mitomed, lower case `ss` is for seconds but it is limited to two digits, Upper case `SSS` is for milliseconds. So make sure your format, that you have been told, is correct. – Habib Sep 16 '14 at 15:28

1 Answers1

5

Looking at the Java date format documentation, I believe you will be receiving 000 through 059 in the seconds part of your string. Also Java doesn't seem to be capable of supplying a tenth of a second.

The number of symbol letters you specify also determines the format.

Number | minimum number of digits is required | shorter numbers are padded with zeros

Thus parsing it as ssf will result in the seconds part of the time being divided by 10.

If this is the case, then the C# format string you need to use is yyyy-MM-ddTHH:mm:0ss.

That said, your "Java format string" contains mm for both month and minute and hh for hours (12-hour) with no indication of an AM/PM specifier, so who knows how reliable it is?

Community
  • 1
  • 1
Rawling
  • 49,248
  • 7
  • 89
  • 127
  • You're right. This is a mess. I will insist on the third party that sent me this (even if I have confirmed that point twice) that there's no typo in it. – mitomed Sep 16 '14 at 15:14
  • @Rawling, one issue though in your linked document there is: "H:mm:ss:SSS 8:28:36:249" , so the seconds value is not exactly between `000 to 059`. `SSS` is for milliseconds, may be upper case lower case difference for seconds and milliseconds. – Habib Sep 16 '14 at 15:23
  • @Habib I don't follow. Multiple `s` is seconds, multiple `S` is milliseconds. More `s` or `S` than required gives leading zeroes. Hence in your example `ss` gives `36`, `SSS` gives `249`; `sss` would give `036`. – Rawling Sep 16 '14 at 18:39
  • @Rawling, "More s or S than required gives leading zeroes" - That is what I missed. You are absolutely right. +1 – Habib Sep 16 '14 at 18:41