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!!!
- Create a NEW Object in DX (see the multitude of other tutorials on this)
- Make it a "TEXT" object, and put in something in the text field, so that it shows on your desktop.
- Add a NEW Script to this object.
- copy/paste the code below into the new object.
Code: vbscript
- Dim BDate
- 'Called when the script is executed
- Sub Object_OnScriptEnter
- BDate = cdate("11/20/2008")
- object.text = "Calculating"
- Object.SetTimer 1, 1000
- End Sub
- Sub Object_OnTimer1
- CurTime = formatdatetime(now,3)
- object.text = "Date: " & formatdatetime(date,1) & vbnewline
- object.text = object.text & "BDate: " & formatdatetime(BDate,1) & vbnewline
- object.text = object.text & "Time: " & CurTime
- i = DateDiff("s", Now, BDate)
- DaysLeft = i \ 86400
- i = i Mod 86400
- HoursLeft = i \ 3600
- i = i Mod 3600
- MinutesLeft = i \ 60
- i = i Mod 60
- SecondsLeft = i
- If len(HoursLeft) < 2 Then HoursLeft = "0" & HoursLeft
- If len(MinutesLeft) < 2 Then MinutesLeft = "0" & MinutesLeft
- If len(SecondsLeft) < 2 Then SecondsLeft = "0" & SecondsLeft
- 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
- End Sub
- 'Called when the script is terminated
- Sub Object_OnScriptExit
- object.KillTimer 1
- 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