Format Duration - CSharp System

CSharp examples for System:DateTime Format

Description

Format Duration

Demo Code

// Permission is hereby granted, free of charge, to any person obtaining
using System.Globalization;
using System;/*from   w  ww  .ja v a 2  s.c  o m*/

public class Main{
        internal static string FormatDuration (int hours, int minutes, int seconds) {
            return (hours > 0 ?
                    String.Format ("{0}:{1:00}:{2:00}", hours, minutes, seconds) :
                    String.Format ("{0}:{1:00}", minutes, seconds));
        }
        internal static string FormatDuration (TimeSpan time) {
            return FormatDuration (time.Hours, time.Minutes, time.Seconds);
        }
        internal static string FormatDuration (long time) {
            return FormatDuration (TimeSpan.FromSeconds (time));
        }
}

Related Tutorials