converting minutes into hrs mins

S

si_owen

Hi folks,

I am returning a value from a db table, which is the number of mins. I
need to convert this to hrs mins which I have done using the following
code, and also displayed it in my grid:

Dim ts As TimeSpan = TimeSpan.FromMinutes(balance)
tempItem.Cells(8).Text = (ts.Hours & ":" & ts.Minutes)

However I have a problem with the minutes, 8:04 is currently displayed
as 8.4, i am using the following code:

Dim Minstring As New StringBuilder
Dim testLength As Integer = CStr(ts.Minutes).Length

If testLength <2 Then
Dim tempMin As String = "0"
tempMin &= ts.Minutes
Minstring.Append(tempMin)
Else
Minstring.Append(ts.Minutes)

End If

!!still displays 8.4, yet this works in a similar project I have when
used exclusively on dates!!

However I also have negative numbers for this value which when
displayed looks like the following:

-1:-23 instead of - 1:23

Does anyone have any ideas on how to remove the " - " sign from mins,
and also put a " 0 " infront of the 4 in 8:4


Any help would be much appreciated,

Cheers,

Simon
 
K

Kelly Ethridge

Hello,

I would write a simple time formatting routine like this:

Private Function FormatTime(ByVal ts As TimeSpan) As String
Return String.Format("{0}:{1:D2}", ts.Hours, ts.Duration.Minutes)
End Function

Kelly
 
O

Oenone

si_owen said:
sorted out the 0 in 8:04

just the - sign now

so -7:-34 shud be -7:34

Use the Math.Abs() function to convert the negative value to a positive one.
Wrap this around the code you're using to extract the minutes from your
TimeSpan and hopefully that'll sort this problem out for you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top