TimeSpan Conversion

2

I'm reading a file delimited by a semicolon and one of the information that is the duration of the link is coming as "00:97:54".

When trying to convert to TimeSpan, it obviously gives error

  

The TimeSpan could not be parsed because at least one of the numeric components is out of range or contain too many digits

How do I convert this time?

    
asked by anonymous 19.05.2017 / 18:31

1 answer

1

It depends on how you have this data, if you are sure that time always comes right it can do like this:

using static System.Console;
using System;

public class Program  {
    public static void Main() {
        var time = "00:97:54".Split(':');
        WriteLine(new TimeSpan(int.Parse(time[0]), int.Parse(time[1]), int.Parse(time[2])));
    }
}

See running on .NET Fiddle . And at Coding Ground . Also put it on GitHub for future reference .

See TryParse() if you can not guarantee the correct format.

    
19.05.2017 / 18:44