Timespan conversion

Asked

Viewed 119 times

2

I am reading a file delimited by 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, 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?

  • Don’t use the answer field to thank, just accept the best answer, as you did and vote for the answers that helped you. If you don’t know how to see the [tour].

1 answer

1


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

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])));
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

See the TryParse() if you cannot guarantee the correct format.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.