A line of code where im trying to create a timespan object which contains "00:00:00.000".
My time format is "HH:mm:ss.fff", took it from a code that works with this exact time format in another project I have.
But it gives a mismatch exception.
Chatgpt and other answers in stackoverflow suggested to use CultureInfo from null to the example below, but it does exactly the same exception.
string timeFormat= "HH:mm:ss.fff";
TimeSpan timeZero = TimeSpan.ParseExact("00:00:00.000", timeformat, CultureInfo.InvariantCulture);
Timespanparsing is little bit different thanDateTimeparsing.From documentation:
There is no custom
HHspecifier forTimeSpan, it should behhinstead. By the waytimeFormatandtimeformatare different variable names, in C#, these names are case sensitive, you need to fix that as well.Here a
demonstration.Be aware of that there is a read-only field as
TimeSpan.Zerowhich you can use as well.As a side note,
CultureInfoalways matters on parsing or displaying operations. When you usenullforCultureInfo, it always uses yourCurrentCultureproperty. That's why your parsing operation might fail if your input string's time separator won't match your current culture'sDateTimeFormatInfo.TimeSeparatorproperty.