ExtensionMethod.NET Home of 875 C#, Visual Basic, F# and Javascript extension methods

SecondsToString

Converts the number of seconds to a string displaying hours and minutes

Source

/// <summary>
/// Converts the seconds to an hour \ min display string.
/// </summary>
/// <param name="totalSeconds">The \total seconds.</param>
/// <returns>A string in the format x hours y mins.</returns>
public static string SecondsToString(this int totalSeconds)
{
    var s = TimeSpan.FromSeconds(totalSeconds);

    return string.Format("{0} hours {1} mins", (int)s.TotalHours, s.Minutes);
}

Example

int seconds = 7863;

string display = seconds.SecondsToString(); // 2 hours 11 mins

Author: K M Thomas

Submitted on: 25 mrt 2016

Language: C#

Type: System.Int32

Views: 5002