How to handle negative TimeSpan value in C#?


I had problems with casting TimeSpan object to DateTime. Error occured: FormatException (“the input string was not in proper format”).

Code I tried:
DateTime.Parse(record.Field(“Hours”)); // raised error

Here record is a DataRow object and I’m using Linq to Objects to parse the Date.

//Field actual value :”{-06:25:00}”

How did I overcome?
I used .Duration() method of TimeSpan object.

DateTime.Parse(record.Field(“Hours”).Duration()); 


HTH 🙂








Leave a Comment