3

I have string like this:"0:385" (first number is seconds, next numbers are milliseconds), I want to use in datagridview with sorting like numbers (not like strings) so I am trying to make DateTime from string but I am not sure how exactly do this. I know I should use method Parse (or ParseExact) but when I tried it it says "String was not recognized as a valid DateTime.". I read some articles about that but I am not sure how exactly use IFormatProvider and next arguments. Thanks for help

4 Answers 4

7

You could use the following: TimeSpan.Parse("0:0:0.365")

But you must format the string like this: [d.]hh:mm:ss[.ff] (The day [d] and fractional seconds [ff] are optional)

See TimeSpan.Parse Method on MSDN

Sign up to request clarification or add additional context in comments.

1 Comment

Thansk, it helps, I think i must repair string to right format.
4

Since you are missing day, month and year from your string, you will not be able to parse it to a DateTime.

Try using a TimeSpan instead.

1 Comment

I tried TimeSpan before but when I use method Parse it says: "The TimeSpan could not be parsed because at least one of the hours, minutes, or seconds components is outside its valid range."
0

TimeSpan.Parse("0:0:0" + str.Replace(':', '.'))

Comments

0

Please try this,

 var dt = new DateTime(2010, 06, 26); // time is zero by default
 string currTime = "19:04:06";//dateTimePicker1.Value.TimeOfDay.ToString();
 var tm = TimeSpan.Parse(currTime);
 var fullDt = dt + tm;  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.