Friday, March 16, 2007

Formatting durations

As the internet gets filled with media files more and more sites need to display the duration of the audio/video file next to the file name and description. We store a file's play duration as a number of seconds in the database. In order to display the duration in a x:xx format we need to format it, using this small function:


private string FormatDuration(short seconds)
{
string result = string.Empty;
int minutes = seconds / 60;
int secondsLeft = seconds % 60;
result = minutes.ToString() + ":" + secondsLeft.ToString("00");
return result;
}

blog comments powered by Disqus