-1

How can convert this string yy-MM-dd HH:mm:ss.SSS to datetime in c#?

Sezer Erdogan
  • 167
  • 1
  • 9
  • 34
  • 3
    Minus one because this question shows no research effort on your part. Surely googling this would have been pretty easy... – rory.ap May 26 '16 at 14:28
  • I didn't answer anything, and your comment doesn't make any sense. If you hover your cursor over the down-vote button, the first thing shown for a reason is "does not show any research effort". – rory.ap May 26 '16 at 14:35

1 Answers1

5

You can use the DateTime.ParseExact() method and define the exact format string that you are expecting to receive :

// This will parse a DateTime object using the "yy-MM-dd HH:mm:ss.fff" format 
var date = DateTime.ParseExact(input,"yy-MM-dd HH:mm:ss.fff", null);

This assumes that the Y characters will represent a two-digit year and the S characters you were looking for would be considered fractional seconds, which have been converted to the y and f formats respectively.

Example

You can see a working example of this here.

Rion Williams
  • 74,820
  • 37
  • 200
  • 327