Search code examples
vb.nett-sqlsql-server-2000excel-2007

Format from normal numbers to time in [hh]:mm format


I usually have this nice way in Excel to convert normal number format to hour and minutes format using simple cell custom formatting [HH]:mm which turns out like this:

excel sheet

Now I can't seem to find a similar to do this in visual basic 2010 or in sql 2000 so does any one know of a way to do it. Thanks in advance.

Note: the brackets in [ HH ]:mm are to keep the hours figure from rounding to 0 after every 24 hour lap.


Solution

  • Try this:

    Public Function FormatHoursAndMinutes(totalMinutes As Integer) As String
        Dim span As TimeSpan = TimeSpan.FromMinutes(totalMinutes)
        Return String.Format("{0:00}:{1:00}", CInt(Math.Floor(span.TotalHours)), span.Minutes)
    End Function