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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Countdown Clock 1

Status
Not open for further replies.

dazzer123

IS-IT--Management
Nov 24, 2003
128
GB
I'm looking to write a program that displays a digital countdown in hous:minutes:seconds and possibly milliseconds to a particular date and time either set in the code or by the user. I started this with the idea that it must be fairly simple but so far i'm stumped, I have no idea how to go about this!!

Any ideas would be really appriciated
 
Ok I got it but I cant seem to work out the milliseconds bit, any ideas?
Code:
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim Holiday As New System.DateTime(2004, 10, 7)
        Dim dtNow As System.DateTime
        Dim Days, Hours, minutes, seconds, milliseconds As String


        Holiday = Holiday.AddHours(7)
        Holiday = Holiday.AddMinutes(15)

        dtNow = System.DateTime.Now

        'MsgBox(dtNow)
        Days = Holiday.Subtract(Now).TotalDays
        Days = Math.Floor(Days)

        Hours = (Holiday.Subtract(Now).TotalHours) - (24 * Days)
        Hours = Math.Floor(Hours)

        minutes = (Holiday.Subtract(Now).TotalMinutes) - (24 * 60 * Days) - (60 * Hours)
        minutes = Math.Floor(minutes)

        seconds = (Holiday.Subtract(Now).TotalSeconds) - (24 * 60 * 60 * Days) - (60 * 60 * Hours) - (60 * minutes)
        seconds = Math.Floor(seconds)

        Me.txtTimeLeft.Text = Days & ":" & Hours & ":" & minutes & ":" & seconds

    End Sub
 
You don't need anything this complicated. The timespan class has loads of useful methods that you can use. Check this out - you need to use math.round just for the hours part, as I have shown below. Also, I generally tend to disable the timer at the start of the call for a timer so that the code gets a chance to execute before the next timer call comes. I've never really investigated what would happen otherwise, but it seems a sensible precaution.

Code:
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Timer1.Enabled = False
        Dim holiday As New DateTime(2004, 10, 7)
        Dim diff As TimeSpan = holiday.Subtract(Now)
        'Dim s As String = diff.TotalHours & ":" & diff.Minutes & ":" & diff.Seconds & "." & diff.Milliseconds
        MessageBox.Show(String.Format("Hours - {0} Minutes - {1} Seconds {2} Milliseconds {3}", Math.Round(diff.TotalHours), diff.Minutes, diff.Seconds, diff.Milliseconds))
        Timer1.Enabled = True
    End Sub

Mark [openup]
 
Thanks for you help, I had never investigated the timespan class before, thats a far simpler way of doing it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top