How can convert this string yy-MM-dd HH:mm:ss.SSS to datetime in c#?
-
3Minus one because this question shows no research effort on your part. Surely googling this would have been pretty easy...rory.ap– rory.ap2016-05-26 14:28:21 +00:00Commented May 26, 2016 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– rory.ap2016-05-26 14:35:29 +00:00Commented May 26, 2016 at 14:35
Add a comment
|
1 Answer
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.
3 Comments
Sezer Erdogan
rion I will try your answer
Rion Williams
I've added an example that you can use to test it out.
Sezer Erdogan
Thanks Rion.it is working fine.Some people dont enough read my question so he give me minus point .But you understand me correct so very thanks .