00:00
00:00
Newgrounds Background Image Theme

Ryor just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

countdown to date

1,280 Views | 5 Replies
New Topic Respond to this Topic

countdown to date 2002-08-04 21:48:24


can anyone help me make a countdown timer to a certain date?

Response to countdown to date 2002-08-05 07:19:18


At 8/4/02 09:48 PM, bigbadron wrote: can anyone help me make a countdown timer to a certain date?

no, but this can.

Response to countdown to date 2002-08-05 11:49:10


At 8/4/02 09:48 PM, bigbadron wrote: can anyone help me make a countdown timer to a certain date?

Let's say that we want to count down to my birthday so everyone knows how much time that they have left to buy me a really good gift. We would have to use the date() function in flash. We start off by defining the current date as the variable 'now' like this:

now = new Date();

My birthday is on September 1st, so let's set a date object for that. We do that with the following syntax: variableName = new Date(year, month [, date [, hour [, minute [, second [, millisecond ]]]]]) Everything after the month is optional, but you cannot skip one to set another. (i.e. you cannot skip date and define hour, it will look at the hour as a date.) Also, the months in flash run from 0-11, so September would be counted as 8. This would define my date in action script as the variable 'later':

later = new Date(now.getFullYear() , 8 , 1);

Now we have two perfectly working dates, how do we compare them? Dates in flash are stored in Unix epoch time, how many milliseconds have passed since Jan. 1, 1970, so we can just subtract the two, and divide by 1000 to get the difference in seconds between the two dates. I have named the variable 'diff' for difference:

diff = int((later - now) / 1000);

That is lovely and all, but who gives a damn about seconds? So now we sort out the seconds, minutes, hours, and days between now and my birthday. I'm using the modulo expression (%) to make things quicker. Modulo returns the remainder of the first expression divided by the second. The seconds are pretty easy to compute like so:

secs = diff % 60;

The rest are going to be a little more difficult. You have to get the seconds from the diff variable, then convert them into minutes. You do it like so:

mins = int((diff % 3600)/ 60);
hours = int((diff % 86400) / 3600);
days = int(diff / (86400));

Now we have all of the information that we need, it is all a matter of putting it together into a readable string. Let's call the string 'left', this is how we will define it:

left = days + " days, " + hours + " hours, " + mins + " minutes, " + secs + ", seconds until my birthday, so go out and get me a good gift now.";

Then put in a play() on that keyframe, and prevFrame();
on the next keyframe to keep the numbers chugging along, and viola! We have a working countdown in flash. All you need to do is set up a dynamic variable to display it, and we are finished.

OK, there is one problem, on September 2nd until January 1st, we are going to get negative number. There are many things that we can do for this. We could put in an if(){} afterward that checks to see if diff is negative, then redefine the date and diff, or just compare now and later, like so:

if (now > later) {
later = new Date(now.getFullYear() + 1 , 8 , 1);
}

So there we have it, now you can have see how much longer you have to get me a good birthday gift in a convenient flash engine. So get cracking.

Here is the whole code for the first frame:

now = new Date();
later = new Date(now.getFullYear() , 8 , 1);
if (now > later) {
later = new Date(now.getFullYear() + 1 , 8 , 1);
}
diff = int((later - now) / 1000);
secs = diff % 60;
mins = int((diff % 3600)/ 60);
hours = int((diff % 86400) / 3600);
days = int(diff / (86400));
left = days + " days, " + hours + " hours, " + mins + " minutes, " + secs + ", seconds until my birthday, so go out and get me a good gift now.";
play();

-Septy.

Response to countdown to date 2002-08-05 15:37:15


At 8/5/02 11:49 AM, Septimus wrote: lots o' stuff
-Septy.

tanks septy! now, do you know how i can make it real time, you know, so it keeps counting down? if you do, thanks, if you dont, thanks anyway. youve been a big help.

{{{septy}}}

Response to countdown to date 2002-08-05 15:52:59


At 8/5/02 03:37 PM, bigbadron wrote: tanks septy! now, do you know how i can make it real time, you know, so it keeps counting down? if you do, thanks, if you dont, thanks anyway. youve been a big help.

{{{septy}}}

As long as you put that in a keyframe and have another keyframe right after it with a prevFrame(); on it, it will count down endlessly. (For example that code is on frame one, then put the prevFrame(); on frame 2.) Or you could use gotoAndPlay(1); to loop it instead. It will loop the count in realtime, but I think that it is based off of the computer that is running the flash, not the server that it is on, so be careful about that...

-Septy.

Response to countdown to date 2002-08-06 00:33:39


At 8/5/02 03:52 PM, Septimus wrote: helpful info
-Septy.

¥A¥! thank you very much!

{{{septy}}}