Latest tutorial: Making a Movieclip face another Movieclip or point on the stage | Ask Tutorial5!
 

Display Date with Adobe Flash 8 and ActionScript

(25 votes)
Written by AnaS   

The Date will be shown in the format Tuesday, January 23 2007.
Create a new document about 250x40.

Draw a text field on the stage using the Text tool from the Tools panel.

In the Properties Inspector set the following for the text field:

W:230; H:30; X:10; Y:5; also set the formatting, I choose Verdana, size 15, color #003366. Don?t forget to select Dynamic Text and to give it the instance name date_txt like in the picture below:

text.jpg

Now insert a new layer and name it actions. Select the first frame of the actions layer and press F9 to open the Actions panel and put there the following code (don't forget that ActionScript is case sensitive):

var date=new Date();
var dayweek=date.getDay();
var dayweek_str:String;
var year=date.getFullYear();
var month=date.getMonth();
var month_str:String;
var days=date.getDate();
var days_str:String;

/* for a nice display I'll use days_str variable for adding a 0 before the days between 1 and 9*/

if (days<10){
days_str="0"+days;
}else{
days_str=days;
}

/*ActionScript sees months like numbers between 0 and 11, in order to display months name I'll use a swich-case block
the month_str variable*/

switch (month){
case(0):month_str="January"; break;
case(1):month_str="February"; break;
case(2):month_str="March"; break;
case(3):month_str="April"; break;
case(4):month_str="May"; break;
case(5):month_str="June"; break;
case(6):month_str="July"; break;
case(7):month_str="August"; break;
case(8):month_str="September"; break;
case(9):month_str="Octomber"; break;
case(10):month_str="November"; break;
case(11):month_str="December"; break;
}

/*ActionScript sees days of the week like numbers between 0 and 6, beggining with Sunday. This problem will be solved in the same manner.*/

switch (dayweek){
case(0):dayweek_str="Sunday"; break;
case(1):dayweek_str="Monday"; break;
case(2):dayweek_str="Tuesday"; break;
case(3):dayweek_str="Wednesday "; break;
case(4):dayweek_str="Thursday"; break;
case(5):dayweek_str="Friday "; break;
case(6):dayweek_str="Saturday"; break;
}

/*now let's display the date in the desired way*/

date_txt.text=dayweek_str+", "+month_str+" "+days_str+" "+year;

Great job! Now test the movie Control >> Test Movie (or CTRL+Enter) and see the result:
You need to Register in order to download the FLA file.



Subscribe now via RSS feed and get all the new tutorials

written by kyutkb , July 07, 2007

hey.. thanks a lot! great help
written by jay , July 10, 2007

how do I add it to my presentation?
written by AnaS , July 20, 2007

You can add this code to a movie clip of your flash presentation. Then put the movie clip wherever you wish in your presentation.

Another possibility is to create a .swf file following the steps described in this tutorial. Then use the loadMovie(url, [method]) method of the MovieClip in Flash.

Hope it was useful!
written by Star , September 14, 2007

Hi. This is great but how can I insert it into a movie without the "background" of the dynamic text. I want a blank text box so that my current background shows through with just text the text over it.

Thank you SO much!!! I learned alot.
written by Star , September 14, 2007

Oh nevermind! I removed the border and now it's transparent. Thanks again for this great tutorial!

Star
written by Scott , October 10, 2007

awesome, simple, tutorial - and it works well.

Thanks!
written by Stephane , October 24, 2007

WOW! I wasn't able to find clear tutorials on internet... but this one is absolutely great!
Thanks a lot!
written by Greg Kozma , October 27, 2007

I am having problems with this everyone else says it is great.Could you send me the fla, so I can see what i am doing wrong

much appreciated
written by Ben McLennan , October 30, 2007

I'm in the same boat as Greg Kozma I'm in my CS3 but the file is set to action script 2 so I shouldn't be having this problem should I? my problem is it just wont display when I test movie...
written by Ben McLennan , October 30, 2007

nevermind, figured it out thanks
written by swearingen , November 03, 2007

slick!

do you have a version for AS3?
written by Karan Sawnani , March 28, 2008

Thanks for the tutorial.

It works great.

There was no problem on Flash CS3 when I used Actionscript 2.0 .
written by mograf designer , July 17, 2008

saved me 2 hours, thanks a million buddy
written by lucy , December 10, 2008

hi there when i adding this tutorial to my current site i am working on i get this message in the out put window...
**Warning** Scene=Scene 1, layer=action1, frame=1:Line 56: Case-insensitive identifier 'date' will obscure built-in object 'Date'.
var date=new Date();

Total ActionScript Errors: 1 Reported Errors: 1

how do i fix it?

thanks
written by jafet , April 17, 2009

Thanks from Mexico!!! Save my day man!
written by rick3556334566655465478756896 , April 23, 2009

okay so I registered and loged in ...where is the sample fla?
written by antoni , May 06, 2009

hi how do i display the year as well mate. great tut by the way
written by SiCooke , May 29, 2009

Brillant tutorial thanks, just one question, does this read the date local to different countries? Where does the info come from?

Do you need more help? Ask now!
 

busy
Last Updated ( Saturday, 22 March 2008 )