-7

I need to convert a string with date to valid c# date time object.

input string example = "2015-03-24T12:31:33.8700000"
output c# datetime

I tried doing this

 DateTime.ParseExact(x.claimDetails.clmDOA, "yyyy-MM-dd HHmmss", CultureInfo.InvariantCulture)

Buit it gave exception

String was not recognized as a valid DateTime

Please Note: I googled thoroghly . Somehow i couldnt find any string with "T" included as in datetime string.

  • Prior to downvoting if one can suggest me exactly where a question is answered with datetime string containg a T in it or of this format.yyyy-MM-dd'T'hh:mm:ss.fffffff
Gyanesh Gouraw
  • 1,991
  • 4
  • 23
  • 31

4 Answers4

3
DateTime dt = DateTime.Parse(example);
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
i486
  • 6,491
  • 4
  • 24
  • 41
  • @SonerGönül In the example the date-time is in ISO format and in this case the culture is not important. – i486 Jan 20 '16 at 12:52
1
DateTime dt = DateTime.ParseExact(example, "yyyy-MM-dd'T'hh:mm:ss.fffffff", CultureInfo.InvariantCulture);

Should work for you.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Arijit Mukherjee
  • 3,817
  • 2
  • 31
  • 51
0

From DateTime.ParseExact documentation;

Converts the specified string representation of a date and time to its DateTime equivalent. The format of the string representation must match a specified format exactly or an exception is thrown.

In your case, they are not.

You need to use T as a string literal delimiter, specify : as a TimeSeparator and seconds fraction (with fffffff specifier) as well.

var dt = DateTime.ParseExact("2015-03-24T12:31:33.8700000", 
                             "yyyy-MM-dd'T'HH:mm:ss.fffffff", 
                             CultureInfo.InvariantCulture);
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • 1
    What's the difference b/w this answer and other one posted by Arijit? – Rahul Jan 20 '16 at 11:36
  • @FrédéricHamidi Since I used `InvariantCulture` as an `IFormatProvider`, I don't think those are the case in here. Of course there is no guarantee to not changing this culture in future but for now, it's ok. – Soner Gönül Jan 20 '16 at 11:36
  • Very true, I missed the `InvariantCulture`. Don't mind me :) – Frédéric Hamidi Jan 20 '16 at 11:37
  • he gave an elaborated explanations and links for the user so that he can learn and stop posting these simple questions, where as My answer so was to solve the issue on urgent basis – Arijit Mukherjee Jan 20 '16 at 11:37
  • @Rahul It uses `hh`, mine uses `HH`. Both can parse `12` but MSDN suggest always using wider ranges. Also little bit explanation as well. – Soner Gönül Jan 20 '16 at 12:13
  • @SonerGönül does hh & HH represents 12 and 24 hour format? – Arijit Mukherjee Jan 21 '16 at 04:57
  • 1
    @ArijitMukherjee Yes, [`hh` specifier](https://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx#hhSpecifier) represents 12-hour format from `01` to `12` but [`HH` specifier](https://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx#HH_Specifier) represents 24-hour format from `00` to `23` – Soner Gönül Jan 21 '16 at 07:09
  • yes, that's exactly what I meant, I didn't knew the exact user requirement so gave the basic one :) – Arijit Mukherjee Jan 21 '16 at 07:20
-1
DateTime.ParseExact(example , "yyyy MM dd HH:mm:ss", CultureInfo.InvariantCulture);
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364