I am trying to convert Time string
to DateTime
but can't.
Example: Time: 12:05:45.458 with milliseconds need to convert to time.
Any solution?
I am trying to convert Time string
to DateTime
but can't.
Example: Time: 12:05:45.458 with milliseconds need to convert to time.
Any solution?
DateTime.ParseExact
should solve this
DateTime result = DateTime.ParseExact("12:05:45.458", "HH:mm:ss.fff", CultureInfo.InvariantCulture);
The DateTime.ParseExtract
will parse the given time and convert to DateTime object with date as current date.
var dateTime = DateTime.ParseExact("12:05:45.458", "HH:mm:ss.fff", System.Globalization.CultureInfo.InvariantCulture);
Console.WriteLine(dateTime);
You are looking for a Timespan.Parse()
here. Since you do not have any date component, it is not possible to unambiguously convert to a DateTime instance.
Simple solution:
string time = "12:05:45.458";
DateTime dateTime = DateTime.ParseExact(time, "HH:mm:ss.fff",
CultureInfo.InvariantCulture);