
Page 1 of 1
Bruno Frulleux
This user is yet to take control of their account and provide a biography. If you are the author of this article, please contact us via support AT actionscript DOT org.
View all articles by Bruno FrulleuxTime: 20 minutes
Difficulty Level: Beginner
Requirements: Flash MX
Topics Covered: Displaying the date and time in a human readable format.
Grab the source for this SWF here.
This tutorial explains how to create a clock which displays the time (hours and minutes) and the date (day of the week, date, month and full year). Because we use switch statements it will work only with Flash MX but you could use if-else-if in place of switch to make it work with Flash 5.
Open a new .fla-file. Create 2 layers, name one "actions" and the other "display". In the actions-layer, create a keyframe in frames 1 and 2. In frame 1 of the actions-layer add the following code into the actions-section
function howlong(arg) {
if (length(arg)==1) {
arg = "0" + arg;
return arg;
}
else {
arg = arg;
return arg;
}
}
This will become clear in a minute, so hang on...
myDate = new Date();
this creates a variable myDate into which we load today's date
hr = howlong(String(myDate.getHours()));
mnt = howlong(String(myDate.getMinutes()));
Here is where the previous function gets called. First, I get the hours from the Date object using myDate.getHours(). I turn this into a string using String(myDate.getHours()). And that, I use as an argument in the functioncall for function howlong.
What happens in this function?
Let's say when I load the hours from the date object it's 8.25 AM. The string which is used as the argument for the function will then be "8". But I want my time to look like this: 08:25. That's what the function does. Let's see what the function would look like using my example:
function howlong(8) {
if (length(8)==1) {
arg = "0" + 8;
return arg;
}
else {
arg = 8;
return arg;
}
}
If the length of the string "8" is 1, then we add a zero before 8 and return this result to whoever called the function, in this case the variable hr. If the length of the string is different from 1, we leave the string alone and return it the way we got it. And pronto! we just made a function to add zero's in front of all single digit numbers! Now we can use this function to add a zero if necessary to the minutes too. That's what happens in:
mnt = howlong(String(myDate.getMinutes()));
Now onto the date.
daytext = myDate.getDay();
dd = myDate.getDate();
mm = myDate.getMonth();
yyyy = myDate.getFullYear();
I load the day, date, month and year into resp. daytext, dd, mm and yyyy. The day is a number from 0 to 6. (0 representing Sunday, 6 representing Saturday). The date is a number from 1 to 31. The month is a number from 0 to 11 (0 representing January, 11 representing December). The year is ... well, it's the year :)
switch (daytext) {
case 0: daytext = "Sunday";
break;
case 1: daytext = "Monday";
break;
case 2: daytext = "Tuesday";
break;
case 3: daytext = "Wednesday";
break;
case 4: daytext = "Thursday";
break
case 5: daytext = "Friday";
break;
case 6: daytext = "Saturday";
break
}
What happens here: I check for which number daytext contains,
If it's 0, I replace daytext with Sunday,
If it's 1, I replace daytext with Monday,
If it's 2, I replace daytext with Tuesday,
etc...
switch (mm) {
case 0: mm = "January";
break;
case 1: mm = "February";
break;
case 2: mm = "March";
break;
case 3: mm = "April";
break;
case 4: mm = "May";
break
case 5: mm = "June";
break;
case 6: mm = "July";
break
case 7: mm = "August";
break
case 8: mm = "September";
break
case 9: mm = "October";
break
case 10: mm = "November";
break
case 11: mm = "December";
break
}
What happens here: I check for which number mm contains,
If it's 0, I replace mm with January,
If it's 1, I replace mm with February,
If it's 2, I replace mm with March,
etc...
[Editor's note from Jesse: You could also achieve the functionality of the above two functions by creating an array of day names and another of month names, and using the number returned for the day/month as an index for the appropriate array. It's just a bit less code, but switch statements are a worthwhile new feature of Flash MX so perhaps you should try using them for experience. I'll shut up now].
textdate = (hr + ":" + mnt + " - " + daytext + "," + dd + " " + mm + " " + yyyy);
And here - at last - we compile the whole shabang! textdate is a variable into which we load a string containing 'hours:minutes - day, date month year', so it could look like this: 08:25 - Thursday, 4 July 2002
And then, to make the time update itself, go to frame 2 in the actions-layer and add the following code:
gotoAndPlay(1);
Go to layer display and create a keyframe in frame 1. Create in frame 1 a textfield, make it a dynamic one, and type at the variable box: textdate.
Tzadaaaa!! You have now linked the textbox with the variable containing our time and date. To make sure the textbox also shows in the second frame, in the display-layer, go to frame 2 and insert a normal frame.
And that's it! When you test the movie, you should now see a clock and today's date, according to your computer. You can off course add seconds to the clock, you can change the date format, ..., but that's up to you!Spread The Word
Related Articles
16 Responses to "Displaying the Date and Time With Flash MX" 
|
said this on 22 Mar 2007 4:22:00 PM CST
Is there a way to make th
|
|
said this on 28 May 2007 12:27:31 PM CST
Excellent work, thank you
I am also wond Thank You! |
|
said this on 13 Jul 2007 10:09:29 PM CST
thanks so good tutorial!!
but i don't unders |
|
said this on 31 Aug 2007 5:07:55 AM CST
Followed all th instructi
|
|
said this on 13 Dec 2007 5:11:02 PM CST
how would you go about us
|
|
said this on 16 Mar 2008 1:21:50 PM CST
Great work, Bruno. I hav
|
|
said this on 11 Sep 2008 12:10:34 AM CST
for 12 hours clock just u
DateX = new Date(); MyH { MyHou } TADAA, l |
|
said this on 03 Dec 2008 2:34:31 PM CST
Or even better
MyHour = |
|
said this on 06 Jan 2009 6:50:11 PM CST
This is great bit of code
Any help would A |
|
said this on 10 Jan 2009 1:10:36 PM CST
Excellent. Thank you.
|
|
said this on 18 Feb 2009 4:57:23 AM CST
When we want the time to
|
|
said this on 01 Sep 2009 4:44:58 AM CST
As I found in another art
So, if you write this: (mm + 1) Try this: hr = my ampm = if (hr>=12) ampm=" hr %= 12; if (hr== hr = howlong If I have |
|
said this on 01 Sep 2009 4:46:45 AM CST
To Adam: If you want "Mor
|
|
said this on 05 Oct 2009 6:54:04 PM CST
ok, question, how would y
|
|
said this on 12 Aug 2010 5:22:22 PM CST
hey, i think if you do no
So OnEnter { } |
|
said this on 28 Oct 2010 9:13:27 AM CST
your scripts didnt wrk fo
|


Author/Admin)