I am given this whole string from database:
CCC_0293170118-10-2013-20-27-54.541
I had the substring so that I get 18-10-2013-20-27-54
, but it throws an error
I want to convert it to DateTime so that I can compare it with another DateTime with format like "{10/18/2013 8:28:46 PM} to get the time difference.
A DateTime
doesn't have a format. It's just a value.
To parse a value such as "18-10-2013-20-27-54" you should use DateTime.ParseExact
(or DateTime.TryParseExact
, if it's somewhat-expected that the data may be invalid). So something like:
DateTime value = DateTime.ParseExact(text, "dd-MM-yyyy-HH-mm-ss",
CultureInfo.InvariantCulture);
It's important to specify the invariant culture here to ensure that the Gregorian calendar is used - if you use the "current system culture" then it could use a different default calendar system.
See more on this question at Stackoverflow