VB icon

Time Conversion

Submitted on: 1/6/2015 7:22:00 AM
By: Terry Hansen (from psc cd)  
Level: Advanced
User Rating: By 4 Users
Compatibility: ASP (Active Server Pages), VbScript (browser/client side)
Views: 838
     To convert a given number into HH:MM:SS
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
' Name: Time Conversion
' Description:To convert a given number into HH:MM:SS
' By: Terry Hansen (from psc cd)
' Inputs:Number of seconds you are looking at converting, and bFixedLength (usually false)
' Assumes:We do alot of maintaining of time in the place I work, and most of the time it is in the format of large integers in number of seconds. This function will take that value and give you back the number of hours, minutes, and seconds in the format HH:MM:SS This is not something that is easily done with either VB formatting or SQL Server formatting...

Public Function CTime(iSeconds, bFixedLength)
Dim intHours As Integer
Dim intMinutes As Integer
Dim intDays As Integer
Dim intSeconds As Integer
Dim lngRemainder As Long
Dim sHours As String
Dim sMinutes As String
Dim sSeconds As String
If IsNull(iSeconds) Then iSeconds = 0
'17 Jun 2002, TLH. This was added to take care of
'negative values. This way the report will still run but
'will show a zero value, and clue in the auditors that
'there is a problem with that particular field...
If iSeconds < 0 Then iSeconds = 0
intHours = Int(iSeconds / 3600)
lngRemainder = CLng(iSeconds Mod 3600)
intMinutes = Int(lngRemainder / 60)
intSeconds = CInt(lngRemainder Mod 60)
sSeconds = String(2 - Len(CStr(intSeconds)), "0") & CStr(intSeconds)
sMinutes = String(2 - Len(CStr(intMinutes)), "0") & CStr(intMinutes)
'sHours = String(2-len(CStr(intHours)), "0") & CStr(intHours)
sHours = CStr(intHours)
CTime = sMinutes & ":" & sSeconds
If intHours > 0 Or bFixedLength Then
CTime = sHours & ":" & CTime
End If
End Function

Other 1 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)

To post feedback, first please login.