Countdown to disaster

Making your own countdown clock

By on October 29, 2008 1:08:41 PM from WinCustomize Forums WinCustomize Forums

RomanDA

Join Date 12/2003
+147

This is the first Tutorial I have done in a while.  I have been working, sick, busy, and to tired to do much of anything for a while.

I am working on a Coundown Clock for the BIRTHDAY community project and I want this to be a multiple part Tutorial on how to make your own.  The First thing we need to do is be able to figure out the Days, Hours, Mins, Seconds between now and Midnight on such-and-such date.  For this example we will use a REALLY important date in DX history... MY BIRTHDAY!!!

  1. Create a NEW Object in DX (see the multitude of other tutorials on this)
  2. Make it a "TEXT" object, and put in something in the text field, so that it shows on your desktop.
  3. Add a NEW Script to this object.
  4. copy/paste the code below into the new object.
Code: vbscript
  1. Dim BDate
  2. 'Called when the script is executed
  3. Sub Object_OnScriptEnter
  4.  BDate = cdate("11/20/2008")
  5.  object.text = "Calculating"
  6.  Object.SetTimer 1, 1000
  7. End Sub
  8. Sub Object_OnTimer1
  9.   CurTime = formatdatetime(now,3)
  10.  object.text = "Date: " & formatdatetime(date,1) & vbnewline
  11.  object.text = object.text & "BDate: " & formatdatetime(BDate,1) & vbnewline
  12.  object.text = object.text & "Time: " & CurTime
  13.      i = DateDiff("s", Now, BDate)
  14.   DaysLeft = i \ 86400
  15.   i = i Mod 86400
  16.   HoursLeft = i \ 3600
  17.   i = i Mod 3600
  18.   MinutesLeft = i \ 60
  19.   i = i Mod 60
  20.   SecondsLeft = i
  21.   If len(HoursLeft) < 2 Then HoursLeft = "0" & HoursLeft
  22.   If len(MinutesLeft) < 2 Then MinutesLeft = "0" & MinutesLeft
  23.   If len(SecondsLeft) < 2 Then SecondsLeft = "0" & SecondsLeft
  24.   object.text = object.text & vbnewline & "Days: " & DaysLeft
  25.   object.text = object.text & vbnewline & "Hours: " & HoursLeft
  26.   object.text = object.text & vbnewline & "Mins: " & MinutesLeft
  27.   object.text = object.text & vbnewline & "Secs: " & SecondsLeft
  28. End Sub
  29. 'Called when the script is terminated
  30. Sub Object_OnScriptExit
  31.  object.KillTimer 1
  32. End Sub

Lets break down the above code.

  • Dim BDate
    This holds a space in the system for BDate so it can be setup in one part of the code and called from another
  • Sub Object_OnScriptEnter
    Runs when the object is loaded

  • BDate = cdate("11/20/2008")
    Sets the Birthday Date to 11/20/2008, you can put in ANY date for now. 
    it needs to be in your "local" format i believe.

  • object.text = "Calculating"
    Default Text to show

  • Object.SetTimer 1, 1000
    This turns on a timer called 1000 and runs what's in there every 1,000 milliseconds or 1 second

  • End Sub
    Ends the OnScriptEnter prrocedure

  • Sub Object_OnTimer1
    This is the Timer we started above, notice the 1 is the same as the 1, in our SetTimer call

  • CurTime = formatdatetime(now,3)
    Sets the Var CurTime to the Current Date in a format that shows hh:mm:ss a/pm

  • object.text = "Date: " & formatdatetime(date,1) & vbnewline
    Sets the Object's text to show the current Date as DayOfWeek, montname, Day, Year

  • object.text = object.text & "BDate: " & formatdatetime(BDate,1) & vbnewline
    Sets the Object's text to show the Birth Date as DayOfWeek, montname, Day, Year
  • object.text = object.text & "Time: " & CurTime
    Shows the Current Time
  • i = DateDiff("s", Now, BDate)
    Set the var i to the difference between NOW and the Birthdate in Seconds
  • DaysLeft = i \ 86400
    Sets the Var DaysLeft the difference between the dates (in seconds) to days
    86400 = 60 seconds * 60 mins * 24 hours
  • i = i Mod 86400
    Sets I down those same seconds into days
  • HoursLeft = i \ 3600
  • i = i Mod 3600
    Makes HoursLeft & I into Hours by deviding it by 3600 (60 seconds * 60 mins)
  • MinutesLeft = i \ 60
    i = i Mod 60
    Makes MinutesLeft & i into Mins by dividing it by 60 (seconds in a min)
  • SecondsLeft = i
    Simple enough I = the seconds left from all that dividing
  • If len(HoursLeft) < 2 Then HoursLeft = "0" & HoursLeft
  • If len(MinutesLeft) < 2 Then MinutesLeft = "0" & MinutesLeft
  • If len(SecondsLeft) < 2 Then SecondsLeft = "0" & SecondsLeft
    If the length of the # is less then 2 characters add the 0 to the begining so it shows 08, 07, 06 etc. no 8 7 6
    Just like it that way, your call
  • object.text = object.text & vbnewline & "Days: " & DaysLeft
  • object.text = object.text & vbnewline & "Hours: " & HoursLeft
  • object.text = object.text & vbnewline & "Mins: " & MinutesLeft
  • object.text = object.text & vbnewline & "Secs: " & SecondsLeft
    Sets the Object's Text to show Days/Hours/mins/Seconds in a row
    the vbnewline is just a linefeed to move the text to the next line
  • object.KillTimer 1
    Turns off the timer when the object closes

When you run the above you should see something that looks like:

Date: Wednesday, October 29, 2008
BDate: Thursday, November 20, 2008
Time: 1:07:36 PM
Days: 21
Hours: 10
Mins: 52
Secs: 24

This is just part 1, I will add more as i move on in making this new countdown gadget.

Hope someone makes use out of this.

RomanDA

 

 

 

 

8 Replies
Search this post
Subscription Options


Reason for Karma (Optional)
Successfully updated karma reason!
October 29, 2008 1:15:03 PM from WinCustomize Forums WinCustomize Forums

Dave, you're a good man!
Thank's for all the great stuff you've made, and all the help you've provided!

Reason for Karma (Optional)
Successfully updated karma reason!
October 29, 2008 1:19:29 PM from WinCustomize Forums WinCustomize Forums

Just what I needed for a project Seabass and I are working on!

 

Reason for Karma (Optional)
Successfully updated karma reason!
October 29, 2008 1:21:50 PM from WinCustomize Forums WinCustomize Forums

Thanks David!

Reason for Karma (Optional)
Successfully updated karma reason!
Sign Up or Login and this ad disappears!
There are many great features available to you once you register. Sign Up for a free account and browse the forums without ads.
October 29, 2008 1:29:03 PM from WinCustomize Forums WinCustomize Forums

Hope you're feeling better!

Thanks for the teaching!

Reason for Karma (Optional)
Successfully updated karma reason!
October 29, 2008 5:19:10 PM from WinCustomize Forums WinCustomize Forums

Part 2 on its way

Reason for Karma (Optional)
Successfully updated karma reason!
October 29, 2008 8:18:45 PM from WinCustomize Forums WinCustomize Forums

Object.SetTimer 1, 1000
This turns on a
timer called 1000 and runs what's in there every 1,000 milliseconds or 1 second

I believe you meant a timer called 1

Reason for Karma (Optional)
Successfully updated karma reason!
October 29, 2008 9:04:11 PM from WinCustomize Forums WinCustomize Forums

yep u r right

Reason for Karma (Optional)
Successfully updated karma reason!
October 29, 2008 10:48:53 PM from WinCustomize Forums WinCustomize Forums

As a relatively new member who has recieved much help from all of your tutorials RomanDA, I just want you to know that there are people out here that really appriciate what you're doing.  Especially those like me who think in more 'visual' terms as an artist and don't do as well with coding.   Thank you.

Reason for Karma (Optional)
Successfully updated karma reason!
Stardock Forums v1.0.0.0    #108431  walnut1   Server Load Time: 00:00:00.0000344   Page Render Time:

Home | About | Privacy | Upload Guidelines | Terms of Service | Help
WinCustomize © 2014 Stardock Corporation. All Rights Reserved.