ToShortName
Converts time span to its largest whole number time unit.
Source
''' <summary>
''' Returns time interval as a string in largest whole number unit.
''' </summary>
''' <param name="inputTimeSpan"></param>
''' <returns>String containing largest divisible time interval.</returns>
''' <remarks>Ex: A time span of 300 seconds will return '5 Minutes'.</remarks>
<Extension()>
Public Function ToShortName(ByVal inputTimeSpan As TimeSpan) As String
Dim returnText As String = String.Empty
Dim interval As Int64 = 0
Dim unitName As String
Try
If inputTimeSpan.Ticks Mod TimeSpan.TicksPerDay = 0 Then
interval = inputTimeSpan.Ticks / TimeSpan.TicksPerDay
unitName = "Day" & If(interval > 1, "s", String.Empty)
ElseIf inputTimeSpan.Ticks Mod TimeSpan.TicksPerHour = 0 Then
interval = inputTimeSpan.Ticks / TimeSpan.TicksPerHour
unitName = "Hour" & If(interval > 1, "s", String.Empty)
ElseIf inputTimeSpan.Ticks Mod TimeSpan.TicksPerMinute = 0 Then
interval = inputTimeSpan.Ticks / TimeSpan.TicksPerMinute
unitName = "Minute" & If(interval > 1, "s", String.Empty)
ElseIf inputTimeSpan.Ticks Mod TimeSpan.TicksPerSecond = 0 Then
interval = inputTimeSpan.Ticks / TimeSpan.TicksPerSecond
unitName = "Second" & If(interval > 1, "s", String.Empty)
ElseIf inputTimeSpan.Ticks Mod TimeSpan.TicksPerMillisecond = 0 Then
interval = inputTimeSpan.Ticks / TimeSpan.TicksPerMillisecond
unitName = "Millisecond" & If(interval > 1, "s", String.Empty)
Else
interval = inputTimeSpan.Ticks
unitName = "Tick" & If(interval = 1, String.Empty, "s")
End If
returnText = String.Format("{0} {1}", interval, unitName)
Catch ex As Exception
returnText = "unavailable"
End Try
Return returnText
End Function
Example
'Following will return "5 Minutes".
Dim intervalName as String = TimeSpan.FromSeconds(300).ToShortName