fpineda101
Well-known member
I am writing a function that returns the time remaining in an auction. The database part is fine, my problem is the calculation of days, hours, minutes and seconds remaining. Below is my code...
Public Function GetTimeRemaining(ByVal inItemNum As Integer) As String
Dim endDate As Date
Dim timeRemaining As Long
Dim timeLeft As Date
Dim timeLeftStr As String
Dim hoursLeft As Integer
Dim daysLeft As Integer
Dim minsLeft As Integer
Dim secsLeft As Integer
endDate = ColumnValue2("joe_barter", "dateCreated + 7", "name = 'fernando'") 'this is a database function - no problems here
secsLeft = DateDiff(DateInterval.Second, Now, endDate)
daysLeft = Floor(secsLeft \ 86400)
secsLeft = 86400 - (86400 / daysLeft) 'daysLeft Mod 86400
hoursLeft = Floor(secsLeft / 3600)
secsLeft = 3600 - (3600 / hoursLeft) 'hoursLeft Mod 3600
minsLeft = Floor(secsLeft / 60)
secsLeft = 60 - (60 / minsLeft) 'minsLeft Mod 60
If secsLeft = 60 Then
secsLeft = 0
minsLeft += 1
End If
If minsLeft = 60 Then
minsLeft = 0
hoursLeft += 1
End If
If hoursLeft = 24 Then
hoursLeft = 0
daysLeft += 1
End If
GetTimeRemaining = CStr(daysLeft) & " days " _
& CStr(hoursLeft) & " hours " _
& CStr(minsLeft) & " minutes " _
& CStr(secsLeft) & " seconds remain..."
daysLeft = 0
hoursLeft = 0
minsLeft = 0
secsLeft = 0
End Function
Public Function GetTimeRemaining(ByVal inItemNum As Integer) As String
Dim endDate As Date
Dim timeRemaining As Long
Dim timeLeft As Date
Dim timeLeftStr As String
Dim hoursLeft As Integer
Dim daysLeft As Integer
Dim minsLeft As Integer
Dim secsLeft As Integer
endDate = ColumnValue2("joe_barter", "dateCreated + 7", "name = 'fernando'") 'this is a database function - no problems here
secsLeft = DateDiff(DateInterval.Second, Now, endDate)
daysLeft = Floor(secsLeft \ 86400)
secsLeft = 86400 - (86400 / daysLeft) 'daysLeft Mod 86400
hoursLeft = Floor(secsLeft / 3600)
secsLeft = 3600 - (3600 / hoursLeft) 'hoursLeft Mod 3600
minsLeft = Floor(secsLeft / 60)
secsLeft = 60 - (60 / minsLeft) 'minsLeft Mod 60
If secsLeft = 60 Then
secsLeft = 0
minsLeft += 1
End If
If minsLeft = 60 Then
minsLeft = 0
hoursLeft += 1
End If
If hoursLeft = 24 Then
hoursLeft = 0
daysLeft += 1
End If
GetTimeRemaining = CStr(daysLeft) & " days " _
& CStr(hoursLeft) & " hours " _
& CStr(minsLeft) & " minutes " _
& CStr(secsLeft) & " seconds remain..."
daysLeft = 0
hoursLeft = 0
minsLeft = 0
secsLeft = 0
End Function