Counting down to a date, such as a product or event launch, in Director is pretty easy using the global systemdate property, however if you want to express the time remaining as anything other than ‘X days to go’ you need to do some number crunching of your own.
This tutorial assumes some basic knowledge of using Director (any version) and fairly basic lingo.
Just to go over the basics, the systemdate property will return something like:
put the systemDate
– date( 2007, 5, 29 )
The date shows year, month, and date. There are a few variations on the command:
put the systemDate.year
– 2007
put the systemDate.month
– 5
put the systemDate.day
– 29
put the systemDate.seconds
– 37400
37400 seconds, which is to 10:23.
Simple differences in dates can be calculated with commands such as:
launchDate = date (2010, 1, 20)
put launchDate - the systemDate
– 967
This tells me that, at the time of writing this, there are only 967 days until a ’significant’ birthday is upon me on 20th Jan 2010.
Of course, 967 days doesn’t mean a great deal to most of us. We’re used to expressing time spans of this length in terms of years, months and days. And, if we want to be really precise, in hours, minutes and seconds.
This isn’t as straightforward as you’d first think, however, due to the Gregorian calendar system that we use, which doesn’t have the year broken into months of equal lengths. If months were equal we could simply divide the number of days to go by the number of days in each months and we’d have our answer. So how do we take 967 days and turn it into years, months and days? We can’t even divide it by 365 to see how many years there are due to the inconvenience of leap years.
When faced with a task like this in Director, or any other language, where there isn’t a built in function ready to solve the problem for you, I always find the best policy is to think how you’d do the maths manually, and then apply that process to your programming.
If I wanted to work this out in my head, I’d work out the years first by adding one each time until I reach the target year, knowing that today’s month is already past the target month, I know to discount this year, so 2008, 2009, 2010 - that’s 2 whole years. Months? I know that today’s date (the 29th) is after the 20th so I can’t include May in my counting, so from June to January - July, August, September, October, November, December, January - 7 months. Days? Add the 2 days left in this month to the 20 in January. That’s 2 years, 7 months and 22 days to go.
Simply converting the target time to seconds after midnight and subtracting the current number of seconds after midnight, and then converting into hours and minutes, we can work the hours and minutes.
Some factors that need consideration are whether there’s only part of a day left to go - i.e. is the time now later in the day than the target time, and is today’s day number later in the month than the target day number. We take care of the latter by storing the days left until the end of this month in a variable, and then counting up the months from the 1st of each one - this will avoid lingo flipping out when you add a month to something like 30th Jan and having a resulting date that simply can’t exist. We also have to consider that the code must deal with the scenario of less than a day to go or the time has already passed. And to finish the job, we should try and provide an answer that’s grammatically correct - outputting “1 days and 1 hours to go” can look pretty stupid.
All very interesting, but how do we do this in lingo? Quite simply, we let the computer do the counting for us, following the same logical approach, and throw in a few checks to make cover all the angles. The handler below may seem a little long-winded, but I’ve done this to make it easier to understand. The target date and time is hard-coded, whereas normally you’d allow scope to pass the date to the handler to make it more flexible. To test this script n Director, just type
Put doCountdown ()
into the message window (having first pasted the script into a movie script in Director, of course). It’ll return the answer in your message window. You can also try a version in your shockwave-enabled browser which allows you to put in your own date (there is only limited error trapping!).
on DoCountdown ()�
— we’ll use the variable pms so we can include the length of time taken to work out the answer in our result
pms = the milliseconds�
launch = date (2010, 1, 20) — my XXth birthday
launchHour = 19 — 7 pm
launchMinutes = 30 — 30 mins past the hour, when I’ll get home from work and celebrate
nowDate = the systemDate — the current date
— look at the hours/mins first
startSecs = (launchHour * 60 * 60) + (launchMinutes * 60)
nowSecs = the systemdate.seconds�
if nowSecs > startSecs then
nowDate = nowDate + 1 — move on a day to compensate for being after launch time
— 86400 is the number of seconds in a day
diffSecs = startSecs + (86400 - nowSecs) — add 19:30’s worth of seconds to the secs remaining today til midnight
hrs = diffSecs / 3600
mins = (diffSecs - (hrs * 3600)) / 60
else
diffSecs = startSecs - nowSecs
hrs = diffSecs / 3600
mins = (diffSecs - (hrs * 3600)) / 60
end if
timeString = hrs & ” hours and ” & mins & ” mins”�
— now the years/months/days
daysLeft = launch - nowDate — the number of days to go�
if daysleft >= 0 and diffSecs >= 0 then
yrs = 0
mnths = 0
days = 0
prevYr = nowDate.year
prevMnth = nowDate.month
prevDay = nowDate.day �
— if the current day is greater than the target day, then we need to store the days remaiming in the current month
— and begin the count forwards from day 1 of the next month
daysToAdd = 0
if nowDate.day > launch.day then �
repeat while (nowdate.day <> 1) and (nowDate < launch)�
nowDate = nowDate + 1 — add one day
daysToAdd = daysToAdd + 1
end repeat
end if
repeat while nowDate < launch
tempdate = duplicate (nowDate) — we’ll increase this tempory variable,
— increase it and see if we overstep the target date �
if tempDate.month < 12 then
tempDate.month = tempDate.month + 1
else
tempDate.month = 1
tempDate.year = tempDate.year + 1
end if
if tempDate < launch then
nowDate = duplicate (tempDate) — we’re still over a month before the target date so we can continue the count
mnths = mnths + 1 — keep a tally of months counted so far
else
— less than a month to go, so work out how many days remain
days = (launch - nowDate) + daysToAdd
exit repeat
end if �
end repeat �
if days < daysToAdd then
days = days + daysToAdd
end if
yrs = mnths/12 — should be obvious! The number of years to go
mnths = mnths - (yrs*12) — again, the obvious way to see how many months we have to go�
— the maths is now done, and we just need to formulate a gramatically correct answer
— make sure the plural usage is correct
case (yrs) of
0: yrString = “”
1: yrString = “1 year “
otherwise: yrString = yrs & ” years “ �
end case
case (mnths) of
0: mnthString = “”
1: mnthString = “1 month “
otherwise: mnthString = mnths & ” months “ �
end case
case (days) of
0: dayString = “”
1: dayString = “1 day “
otherwise: dayString = days & ” days “ �
end case �
return ( yrstring & mnthstring & daystring & timeString & ” to go. Worked out in ” & the milliseconds - pMs & ” milliseconds”)
else
return (”The date has passed!”)
end if
end







