I’m doing up an application that converts the STRING duration of a media file in C#.
How do I make it so that the output are as follows:
a. 1:34 (1min 34 sec) to
00:01:34
b. 0:05 (5 seconds) to
00:00:05
c. 1:10:05 to 01:10:05
Result will be displayed in a label named lblDuration.
I am using VS2008 C#.
Thanks if you can help.
I would parse it as a
TimeSpan, and then reformat it usingThis is assuming you’re using .NET 4 with its support for custom
TimeSpanformats. Before .NET 4 you’d have to write it yourself – not too hard, but harder than the above.Parsing the timespan to start with is a different matter – you could use
TimeSpan.TryParseExactpassing in multiple formats, for example.The benefit of parsing and then reformatting is that you’ll validate that you’ve got sensible date – e.g. not “99:99”.