Returns how long something took in sec, minutes, hours, or days - CSharp System

CSharp examples for System:DateTime Minute

Description

Returns how long something took in sec, minutes, hours, or days

Demo Code


using System;//from w  w w.j a v  a2  s.  c om

public class Main{
        /// <summary>
        /// Returns how long something took in sec, minutes, hours, or days
        /// </summary>
        public static string TimeTaken(this TimeSpan time)
        {
            string output = "";
            if (time.Days > 0)
                output += time.Days + " day" + (time.Days > 1 ? "s " : " ");
            if ((time.Days == 0 || time.Days == 1) && time.Hours > 0)
                output += time.Hours + " hour" + (time.Hours > 1 ? "s " : " ");
            if (time.Days == 0 && time.Minutes > 0)
                output += time.Minutes + " minute" + (time.Minutes > 1 ? "s " : " ");
            if (output.Length == 0)
                output += time.Seconds + " second" + (time.Seconds > 1 ? "s " : " ");
            return output.Trim();
        }
}

Related Tutorials