Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

TimeSpan.Parse of string including days dd-hh:mm:ss

The time span I want to parse is of the format dd-hh:mm:ss
It is the output by the Linux command that returns how long a process has been running.

Example:

string s = "5-15:10:20"; // 5 days 15h 10m 20s
TimeSpan.Parse(s);

This generates the error

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

System.FormatException was unhandled
Message="Input string was not in a correct format."
Source="mscorlib"
StackTrace:
   at System.TimeSpan.StringParser.Parse(String s)
   at System.TimeSpan.Parse(String s)

Important note: Code to be written in .net 2.0
Is there a way to let the TimeParse correctly identify the first date part?

Edit: I tried replacing the ‘-‘ with ‘:’ but it gives the same error.

>Solution :

The problem with your string is that the char "-" is a optional minus sign, which indicates a negative TimeSpan. So, you have to use the Replace() method before parsing your string.

On this link you can see all the common chars that works with that method. For that, something like that will work:

string s = "5-15:10:20"; // 5 days 15h 10m 20s
TimeSpan.Parse(s.Replace('-', '.'));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading