Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Countdown timer to specific date/time (dd:hh:mm:ss) 1

Status
Not open for further replies.

postmanphat

Technical User
Nov 13, 2006
117
GB
Afternoon all....

I've had a poke around on here but can't quite find the answer I'm looking for.

I'd like to be able to show on a form how many days, hours, minutes and seconds there are until 5pm on May 30th? I'd like this to countdown 'live' while the form is open too.

Actually the specific format isn't so important, as long as that info is presented in one way or another.

Many thanks in advance as always,

Dave
 
Create a form with a timer event and text box

and on timeer event
Code:
me.text0 =Datediff("s",now(),#5/31/10#)

this will give youa countdown of seconds
 
Hi PWise,

That will just give me however many millions of seconds there are until I guess 1 second past midnight on the 31st May. I need it to tell me there are 24 days, 1 hour, 12 minutes, 14 seconds or whatever to go....
 
A starting point:
DateDiff("d",Now(),#2010-05-31 23:59:59#) & "d, " & Datediff("h",Now(),#2010-05-31 23:59:59#) Mod 24 & "h, " & DateDiff("n",Now(),#2010-05-31 23:59:59#) Mod 60 & "m, " & DateDiff("s",Now(),#2010-05-31 23:59:59#) Mod 60 & "s"

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi. OP here...

I've discovered a few minor problems in the code above - if the 'target' date is less than 24 hours away it will still say "1 day 23 hours xx minutes...." instead of "0 days 23 hours....".

And on some times it doesn't calculate the hours properly in that it seems that the hours value is a rounded-up figure so that it sometimes says there are "8 hours 23 minutes" to go when they are in fact "7 hours 23 minutes".

Any further advice is greatly appreciated.

Thanks

Dave
 
This is a function that I found some time ago. The original web page has disappeared so I'll just post the whole function
Code:
Public Function Diff2Dates(Interval As String, Date1 As Date, Date2 As Date, _
                           Optional ShowZero As Boolean = False) As Variant
    'Authors:   © Copyright 2001.
    '           Graham R Seach MCP MVP  gseach@pacificdb.com.au
    '           Douglas J. Steele MVP
    '
    '           You may freely use and distribute this code
    '           with any applications you may develop, on the
    '           condition that the copyright notice remains
    '           unchanged, and intact as part of the code. You
    '           may not sell or publish this code in any form
    '           without the express written permission of the
    '           copyright holders.
    '
    'Description:   This function calculates the number of years,
    '               months, days, hours, minutes and seconds between
    '               two dates, as elapsed time.
    '
    'Inputs:    Interval:   Intervals to be displayed (a string)
    '           Date1:      The lower date (see below)
    '           Date2:      The higher date (see below)
    '           ShowZero:   Boolean to select showing zero elements
    '
    'Outputs:   On error: Null
    '           On no error: Variant containing the number of years,
    '               months, days, hours, minutes & seconds between
    '               the two dates, depending on the display interval
    '               selected.
    '           If Date1 is greater than Date2, the result will
    '               be a negative value.
    '           The function compensates for the lack of any intervals
    '               not listed. For example, if Interval lists "m", but
    '               not "y", the function adds the value of the year
    '               component to the month component.
    '           If ShowZero is True, and an output element is zero, it
    '               is displayed. However, if ShowZero is False or
    '               omitted, no zero-value elements are displayed.
    '               For example, with ShowZero = False, Interval = "ym",
    '               elements = 0 & 1 respectively, the output string
    '               will be "1 month" - not "0 years 1 month".

    ' Additional changes:
    ' 1) Formats associated with date segments.
    ' 2) Changed order of arguments.
    ' 3) Variables renamed. Constants replaced by text.
    '    Output sign detection changed (+ or -).
    '    Comments added. Copyright changed.

    On Error GoTo Diff2Dates_ErrorHandler

    Dim vTemp                       As Variant        'A general purpose variant
    Dim lYearDiff                   As Long           'Difference in years
    Dim lMonthDiff                  As Long           'Difference in months
    Dim lDayDiff                    As Long           'Difference in days
    Dim lHourDiff                   As Long           'Difference in hours
    Dim lMinDiff                    As Long           'Difference in minutes
    Dim lSecDiff                    As Long           'Difference in seconds
    Dim bYear                       As Boolean        'Boolean - Was year output selected?
    Dim bMonth                      As Boolean        'Boolean - Was month output selected?
    Dim bDay                        As Boolean        'Boolean - Was day output selected?
    Dim bHour                       As Boolean        'Boolean - Was hour output selected?
    Dim bMinute                     As Boolean        'Boolean - Was minute output selected?
    Dim bSecond                     As Boolean        'Boolean - Was second output selected?
    Dim iCtr                        As Integer        'General purpose counter
    Dim dTmpDate                    As Date           'A temporary date container
    Dim bSwapped                    As Boolean        'Boolean flag indicating whether the dates have been swapped

    Const INTERVALS                 As String = "dmyhns"
    'Check that Interval contains valid characters
    Interval = Trim(LCase(Interval))
    For iCtr = 1 To Len(Interval)
        If InStr(1, INTERVALS, Mid(Interval, iCtr, 1)) = 0 Then
            GoTo Diff2Dates_ErrorHandler
        End If
    Next iCtr

    'Check that valid dates have been entered
    If Not (IsDate(Date1)) Then Exit Function
    If Not (IsDate(Date2)) Then Exit Function

    'If necessary, swap the dates, to ensure that
    'Date1 is lower than Date2
    If Date1 > Date2 Then
        dTmpDate = Date1
        Date1 = Date2
        Date2 = dTmpDate
        bSwapped = True
    End If

    vTemp = Null

    'What intervals are supplied
    bYear = (InStr(1, Interval, "y") > 0)
    bMonth = (InStr(1, Interval, "m") > 0)
    bDay = (InStr(1, Interval, "d") > 0)
    bHour = (InStr(1, Interval, "h") > 0)
    bMinute = (InStr(1, Interval, "n") > 0)
    bSecond = (InStr(1, Interval, "s") > 0)

    'Debug.Print "Date1: " & Date1
    'Debug.Print "Date2: " & Date2

    'Get the cumulative differences between the two dates
    If bYear Then
        lYearDiff = Abs(DateDiff("yyyy", Date1, Date2) - _
                        IIf(Format(Date1, "mmddhhnnss") <= Format(Date2, "mmddhhnnss"), 0, 1))
        Date1 = DateAdd("yyyy", lYearDiff, Date1)
    End If

    If bMonth Then
        lMonthDiff = Abs(DateDiff("m", Date1, Date2)) - _
                     IIf(Format(Date1, "ddhhnnss") <= Format(Date2, "ddhhnnss"), 0, 1)
        Date1 = DateAdd("m", lMonthDiff, Date1)
    End If

    If bDay Then
        lDayDiff = Abs(DateDiff("d", Date1, Date2)) - _
                   IIf(Format(Date1, "hhnnss") <= Format(Date2, "hhnnss"), 0, 1)
        Date1 = DateAdd("d", lDayDiff, Date1)
    End If

    If bHour Then
        lHourDiff = Abs(DateDiff("h", Date1, Date2)) - _
                    IIf(Format(Date1, "nnss") <= Format(Date2, "nnss"), 0, 1)
        Date1 = DateAdd("h", lHourDiff, Date1)
    End If

    If bMinute Then
        lMinDiff = Abs(DateDiff("n", Date1, Date2)) - _
                   IIf(Format(Date1, "ss") <= Format(Date2, "ss"), 0, 1)
        Date1 = DateAdd("n", lMinDiff, Date1)
    End If

    If bSecond Then
        lSecDiff = Abs(DateDiff("s", Date1, Date2))
        Date1 = DateAdd("s", lSecDiff, Date1)
    End If

    'Set the output display
    If bYear And (lYearDiff > 0 Or ShowZero) Then
        vTemp = lYearDiff & IIf(lYearDiff <> 1, " years", " year")
    End If

    If bMonth And (lMonthDiff > 0 Or ShowZero) Then
        If bMonth Then
            vTemp = vTemp & IIf(IsNull(vTemp), Null, " ") & _
                    lMonthDiff & IIf(lMonthDiff <> 1, " months", " month")
        End If
    End If

    If bDay And (lDayDiff > 0 Or ShowZero) Then
        If bDay Then
            vTemp = vTemp & IIf(IsNull(vTemp), Null, " ") & _
                    lDayDiff & IIf(lDayDiff <> 1, " days", " day")
        End If
    End If

    If bHour And (lHourDiff > 0 Or ShowZero) Then
        If bHour Then
            vTemp = vTemp & IIf(IsNull(vTemp), Null, " ") & _
                    lHourDiff & IIf(lHourDiff <> 1, " hours", " hour")
        End If
    End If

    If bMinute And (lMinDiff > 0 Or ShowZero) Then
        If bMinute Then
            vTemp = vTemp & IIf(IsNull(vTemp), Null, " ") & _
                    lMinDiff & IIf(lMinDiff <> 1, " minutes", " minute")
        End If
    End If

    If bSecond And (lSecDiff > 0 Or ShowZero) Then
        If bSecond Then
            vTemp = vTemp & IIf(IsNull(vTemp), Null, " ") & _
                    lSecDiff & IIf(lSecDiff <> 1, " seconds", " second")
        End If
    End If
    '************************************************************

    If bSwapped Then vTemp = "-" & vTemp
    Diff2Dates = Trim(vTemp)
    '    Debug.Print vTemp
    '    Debug.Print Trim(vTemp)

Exit_Diff2Dates:
    Exit Function

Diff2Dates_ErrorHandler:
    Diff2Dates = Null
End Function
It gives you lots of options about how to format the returned date/time string.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top