TimeSpan issue...

newguy

Well-known member
Joined
Jun 30, 2008
Messages
611
Location
Denver Co, USA
Programming Experience
1-3
Can someone tell me why this does not work.
VB.NET:
Public Function UnitTimeValue(ByVal time As Double) As String
   Dim ts As TimeSpan = TimeSpan.FromMinutes(time)
   Return ts.Hours & ":" & ts.Minutes.ToString("0#") & ":" & ts.Seconds.ToString("0#")
End Function
If I type in 450.50 I get [7:30:30], but when I type in 4500.50 I get [3:00:30]
Very confusing...
 
This works:
VB.NET:
Return Fix(ts.TotalHours).ToString & ":" & Fix(ts.TotalMinutes).ToString("0#") & ":" & (ts.Seconds).ToString("0#")
Duh moment...
 
You don't ever need to use Fix and what you're doing there is not correct. You do need to use TotalHours but you don't need to use TotalMinutes:
VB.NET:
Public Function UnitTimeValue(ByVal totalMinutes As Double) As String
    Dim time As TimeSpan = TimeSpan.FromMinutes(totalMinutes)

    Return String.Format("{0}:{1:00}:{2:00}", _
                         Math.Floor(time.TotalHours), _
                         time.Minutes, _
                         time.Seconds)
End Function
 
Thanks JMC, I admit TimeSpans are not my forte. I found the values I needed while debugging.
 
Back
Top