-7

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?

Bojjaiah
  • 241
  • 2
  • 7
  • 25

4 Answers4

2

DateTime.ParseExact should solve this

DateTime result = DateTime.ParseExact("12:05:45.458", "HH:mm:ss.fff", CultureInfo.InvariantCulture);
fubo
  • 44,811
  • 17
  • 103
  • 137
1

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);
user1672994
  • 10,509
  • 1
  • 19
  • 32
1

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.

Tanveer Badar
  • 5,438
  • 2
  • 27
  • 32
-2

Simple solution:

 string time = "12:05:45.458";
    DateTime dateTime = DateTime.ParseExact(time, "HH:mm:ss.fff",
                                            CultureInfo.InvariantCulture);