user2284452
user2284452

Reputation: 135

Parse or Convert TimeSpan to string

I need to parse a TimeSpan to a string using a user provided format

I've tried a number of options including various RegEx.Replace and Parse options.

In the below I've added an Extension method to TimeSpan.

public void should_return_hours_and_minutes_and_seconds()
{
    _ts = new TimeSpan(2, 1, 30, 10);

    var format = "HH:mm:ss"; // Input by User. Goal is to include other formats

    var returnedVal = _ts.ToString(format);

    Assert.That(returnedVal, Is.EqualTo("49:30:10")); // Days rollup to hours
}

Upvotes: 2

Views: 718

Answers (1)

Shyam sundar shah
Shyam sundar shah

Reputation: 2523

Try to use this code

public void should_return_hours_and_minutes_and_seconds()
{
    DateTime d = new DateTime(time_span.Ticks);
    string time = d.ToString("HH:mm:ss");
}

Upvotes: 1

Related Questions