[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 321
  • Last Modified:

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
0
acaraciolo
Asked:
acaraciolo
  • 2
1 Solution
 
Ashish PatelCommented:
   lblDay = dspDay & " days,"
    lblHour = dspHour & " hours,"
    lblMin = dspMinute & " minutes,"
    lblSec = dspSecond & " seconds"
0
 
JohnK813Commented:
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
 
acaracioloAuthor 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 -

the program read
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

Open in new window

0
 
JohnK813Commented:
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

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now