Wednesday, 9 December 2009

Display date in Actionscript 3

In this tutorial you will learn how to display the date in Actionscript 3. This is an update from the display date tutorial in AS2. To display the date you need to use the Date class which will retrieve all date details such as the months, the years and the days. If you have completed the AS2 tutorial all the code below should look familiar to you.


Display date in Actionscript 3

Step 1

Open a new Flash AS3 file.
Select the Text tool (T) with dynamic text and create a small rectangle shape on the stage like below.



You may need to embed the following glyphs by selecting the ‘Character Embedding’ button: Uppercase, Lowercase, Numerals and Punctuation.


Step 2

Give your dynamic text field the following instance name shown below:




Step 3

On the timeline create a new layer called ‘Actions’, and open up actions panel (F9) and enter the following code.

//Array to hold a list of the weekdays.
var weekdays:Array = new Array ("Sunday","Monday","Tuesday","Wednesday",
"Thursday","Friday","Saturday");

//Array to hold a list of the months.
var months:Array = new Array ("Jan","Feb","Mar","Apr","May","Jun","Jul",
"Aug", "Sep", "Oct","Nov","Dec");

//Adds an event listener to the dymanic text field.
the_date.addEventListener(Event.ENTER_FRAME,showDate);

function showDate(event:Event):void {
//Create a new instance of the date class.
var myDate:Date = new Date();

//Retrieve the day, month and year from the date class.
var theDay=weekdays[myDate.getDay()];
var theMonth=months[myDate.getMonth()];
var theDate=myDate.getDate();
var theYear=myDate.getFullYear();

//Display the date in the dynamic text field.
the_date.text=theDay+", "+theMonth+" "+theDate+", "+theYear;
}

Step 4

Test your movie clip Ctrl + Enter.



You should now be able to display the date in Actionscript 3.0.

4 comments:

h3lLx0x 10 December 2009 09:49  

what flash version are you using ? nice tutorial though.

iliketo 11 December 2009 06:01  

@h3lLx0x

You can use either CS3 or CS4, if you choose the Actionscript 3 file.

AradiaDawn 7 February 2010 09:58  

Thanks for the great tutorial!

I'm curious, though, how would I alter this to display the date for exactly one week from today?

iliketo 12 February 2010 09:41  

@AradiaDawn

Use the following.

var theDate=myDate.getDate()+ 7;

  COPYRIGHT © 2011 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP