Home All Groups Group Topic Archive Search About

converting minutes into hrs mins

Author
19 Dec 2006 1:49 PM
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

Author
19 Dec 2006 2:24 PM
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

si_owen wrote:
Show quoteHide quote
> 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
>
Author
19 Dec 2006 3:09 PM
si_owen
sorted out the 0 in 8:04

just the - sign now

so -7:-34 shud be -7:34
Author
19 Dec 2006 3:33 PM
Oenone
si_owen wrote:
> 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.

--

(O)enone