Calculating time remaining

fpineda101

Well-known member
Joined
Nov 14, 2005
Messages
122
Location
Los Angeles, CA
Programming Experience
1-3
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
 
Research "timespan". There are examples in this forum where calculations are made with time.

Something along these lines:

Dim Auction As DateTime
Dim dt As DateTime = DateTime.Now
Auction = 'set the auction ending date and time
Dim ts As TimeSpan = Auction.Subtract(dt)

'Format 'ts' however you want and display it.


P.S. Please update your primary platform info in your profile.
 
Back
Top