Categories
Featured jobs
» More ActionScript, Flash and Flex jobs.
» Advertise a job for free
Our network
Advertisement

 »  Home  »  Tutorials  »  Flash  »  Beginner  »  Displaying the Date and Time With Flash MX

Displaying the Date and Time With Flash MX

By Bruno Frulleux | Published 09/9/2005 | Beginner | Rating:
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 Frulleux
Page 1 of 1
Written by: Bruno Frulleux
Time: 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);

This ensures that the Flashmovie will loop over and over again, fetching the time and date from the computer, thus actively updating it on your screen! Now all we still need is a place to show our date and time contraption.

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!
How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent

Verification:
Enter the security code shown below:
img


Add comment

Spread The Word / Bookmark this content

Clesto Digg it! Reddit Furl del.icio.us Spurl Yahoo!

Related Articles
Comments
  • Comment #1 (Posted by robert - robert at independent.com)
    Rating
    Is there a way to make the time display in 12 hr format (not millitary time)? Thanks!
     
  • Comment #2 (Posted by Andrew)
    Rating
    Excellent work, thank you for taking the time to do this!

    I am also wondering the same thing as the person above me, is it possible to have it on a 12h clock instead of a 24?

    Thank You!
     
  • Comment #3 (Posted by vicky - kivick at hotmail.com)
    Rating
    thanks so good tutorial!!
    but i don't understand why use"arg" in function part? what's that mean??
     
  • Comment #4 (Posted by Jo - joe_bateman3 at hotmail.com)
    Rating
    Followed all th instructions, and my time is 12 hours infront on the actual time. Any ideas why? and how can I correct this?
     
  • Comment #5 (Posted by Dean - kramerd at seattleu.edu)
    Rating
    how would you go about using the date to jump to a paticular frame
     
  • Comment #6 (Posted by M-Auwal Gene III - www.gene3 [at] yahoo.com)
    Rating
    Great work, Bruno. I have used your ideas to build a single-parameter function that grabs the system date, formats it in several ways, and return it as string. (eg: "26/03/2008", "16-Mar-2008", "Saturday, 16 March 2008", etc).
     
Submit Comment



Search Entire Site
Add to Google
Advertisements
Article Options
Latest New Articles
Set up a simple IIS Server for Flash
by Peter McBride

Day 1 at FITC Toronto 2008
by Anthony Pace

Simple reflection effect with AS2
by Jean André Mas

ActionScript.org Meets Josh Tynjala (aka dr_zeus)
by ActionScript.org Staff

Rapidly Create Online Flash Movies to Help Users Market, Sell and Support Software and Hardware
by Sabrina F

mailing list
Enter your email address:
mailing list
Subscribe Unsubscribe
© 2000-2007 actionscript.org! All Rights Reserved.
Read our Privacy Statement and Terms of Use...
Our dedicated server is hosted and managed by WebScorpion Webhosting.