# Days, Hours, Minutes, Seconds

I am wanting to display a running countdown between Today and some date in the future.  I can use datDiff and get Day Hour Minute Second.  It works fine for Day but hours, minutes, and seconds is cumlitive.  For example in this code:
Dim curDate As Date
Dim fltDate As Date
Dim dspDay As Long
Dim dspHour As Long
Dim dspMinute As Long
Dim dspSecond As Long

fltDate = "6/24/2008 6:45:00 AM"

dspDay = DateDiff("d", Date, fltDate)
dspHour = DateDiff("h", Date, fltDate)
dspMinute = DateDiff("n", Date, fltDate)
dspSecond = DateDiff("s", Date, fltDate)

lblDay = dspDay
lblHour = dspHour
lblMin = dspMinute
lblSec = dspSecond

I get:
Day 146
Hour 3,510
Minute 210,645
Seconds 263,870

How do I massage those numbers so they display the correct difference like 146 days, 22 hours, 49 minutes, 58 seconds
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
lblDay = dspDay & " days,"
lblHour = dspHour & " hours,"
lblMin = dspMinute & " minutes,"
lblSec = dspSecond & " seconds"
0
Commented:
Try this.

dspDay = DateDiff("d", Date, fltDate)
dspHour = DateDiff("h", Date, fltDate) - (dspDay * 24) 'total hours - hours in dspDay
dspMinute = DateDiff("n", Date, fltDate) - (dspDay * 24 * 60) - (dspHour * 60) 'total minutes - minutes in dspDay - minutes in dspHour
dspSecond = DateDiff("s", Date, fltDate) - (dspDay * 24 * 60 * 60) - (dspHour * 60 * 60) - (dspMinute * 60) 'total seconds - seconds in dspDay - seconds in dspHour - seconds in dspMinute
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Your math worked fine and is the solution I will accept but it brought up something different.  I'll ask you and if you think it should be a totally seperate question with seperate points I will certaainly agree.  When I substituted your changes the numbers looked right accept minutes and seconds were showing as -

146     -9      25    -52

the seconds counted up from 1 to 59
``````Private Sub Timer1_Timer()

dspDay = DateDiff("d", Now(), fltDate)
dspHour = DateDiff("h", Now(), fltDate) - (dspDay * 24) 'total hours - hours in dspDay
dspMinute = DateDiff("n", Now(), fltDate) - (dspDay * 24 * 60) - (dspHour * 60) 'total minutes - minutes in dspDay - minutes in dspHour
dspSecond = DateDiff("s", Now(), fltDate) - (dspDay * 24 * 60 * 60) - (dspHour * 60 * 60) - (dspMinute * 60) 'total seconds - seconds in dspDay - seconds in dspHour - seconds in dspMinute

lblDay = dspDay
lblHour = dspHour
lblMin = dspMinute
lblSec = dspSecond

DoEvents: DoEvents: DoEvents

End Sub
``````
0
Commented:
I think I got it, thanks to this previous solution: http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_23075507.html

Also, I set Now() to a variable, just so all of your fields are using the same definition of Now().  (Instead of dspDay using 1/29/2008 04:24:00 and dspSecond using 1/29/2008 04:24:02, for example.)

Try this:
``````    curDate = Now()
fltDate = "6/24/2008 6:45:00 AM"

dspDay = DateDiff("d", Now(), fltDate)
If Time(fltDate) < Time(curDate) Then
'If we have already passed the target time today
'(ie, if it is after 6:45 AM today),
'subtract one day.
dspDay = dspDay - 1
End If
dspHour = DateDiff("h", Now(), fltDate)
dspHour = dspHour Mod 24
dspMinute = DateDiff("n", Now(), fltDate)
dspMinute = dspMinute Mod 60
dspSecond = DateDiff("s", Now(), fltDate)
dspSecond = dspSecond Mod 60
``````
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.