Reputation: 25
I want to convert total minutes to hours and minutes hh:mm..
This is my code
Private Sub TextBox5_TextChanged(sender As Object, e As EventArgs) Handles TextBox5.TextChanged
Dim TotalMinute As Int32
Dim Minute As Int32
Dim Hour As Int32
Try
TotalMinute = CType(TextBox9.Text, Int32)
TotalMinute = TotalMinute Mod 1440
Hour = TotalMinute \ 60
Minute = TotalMinute Mod 60
TextBox5.Text = FormatTwoDigits(Hour) & ":" & FormatTwoDigits(Minute)
Catch ex As Exception
Exit Sub
End Try
End Sub
Private Function FormatTwoDigits(ByVal i As Int32) As String
If 30 > i Then
FormatTwoDigits = "0" & i.ToString
Else
FormatTwoDigits = i.ToString
End If
End Function
This code works fine under 24 hours.. but counts back from 0 after 24...
For example if the input is 1500 minutes it should say 25:00 not 01:00
Upvotes: 0
Views: 7622
Reputation: 11773
Or you could let .Net do the work
Dim ts As TimeSpan = TimeSpan.FromMinutes(TotalMinute)
Dim s As String = String.Format("{0:00}:{1:00}", Math.Floor(ts.TotalHours), ts.Minutes)
Upvotes: 5
Reputation: 39122
I'd do something more like:
Private Sub TextBox5_TextChanged(sender As Object, e As EventArgs) Handles TextBox5.TextChanged
Dim TotalMinute As Integer
If Integer.TryParse(TextBox9.Text, TotalMinute) Then
Dim ts As TimeSpan = TimeSpan.FromMinutes(TotalMinute)
TextBox5.Text = CType(ts.TotalHours, Integer).ToString("00") & ":" & ts.Minutes.ToString("00")
End If
End Sub
Upvotes: 0
Reputation: 23974
Remove your TotalMinute = TotalMinute Mod 1440
line, which is saying to get rid of any "day" portion if the total minutes exceeds one day. (60 minutes = 1 hour, 24 hours = 1 day, therefore 1440 minutes = 1 day.)
Upvotes: 3