SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
ASP.NET, VB.NET, Microsoft .NET Framework, Microsoft Visual Studio, Windows Forms, Controls and more Tutorials and Articles for Programmers


Format Minute to Hours in VB.NET


Here is a sample VB.NET code which can be used to format a number of minutes into hours in two digits and minutes in two digits also. In short the resultant format is in hours:minutes structure.
For example a time span of 1439 minutes will be formatted to "23:59" and 120 minutes is going to be formatted like "02:00". A period of 5 minutes is going to be converted to a string value of "00:05". A full day period 1440 minutes is converted by the formatting function to a value "00:00"
Below is the Visual Basic .NET code for the function and the sample ASP.NET web page which has two textboxes and a button on it. When an integer value is entered into textbox1 and then the button is pressed the formatted or the converted string value is displayed on the textbox2.





Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

     Dim TotalMinute As Int32

     Dim Minute As Int32
     Dim Hour As Int32

     Try
          TotalMinute = CType(TextBox1.Text, Int32)

          TotalMinute = TotalMinute Mod 1440

          Hour = TotalMinute \ 60
          Minute = TotalMinute Mod 60

          TextBox2.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 10 > i Then
          FormatTwoDigits = "0" & i.ToString
     Else
          FormatTwoDigits = i.ToString
     End If
End Function



Visual Studio


Copyright © 2004 - 2021 Eralper YILMAZ. All rights reserved.