- Open a new Flash document and change the name of Layer 1 to Text.
- In the Text layer, create a dynamic text field and assign it the instance name of currentDate_txt.
- Create an Actions layer.
With Frame 1 of the Actions layer selected, open the Actions panel.
- Create, or instantiate, an object from the Date class, named myDate:
var myDate:Date=new Date();
- Create a variable called currentMonth equal to the getMonth() method:
var currentMonth:Number = myDate.getMonth();
- Trace the value of currentMonth:
trace (currentMonth);
- Save and test the document.
You should see a number in the Output panel that represents the month.
The getMonth() method displays the current month. The getMonth() method is zero-indexed, meaning the numbering begins at zero rather than one, so the number displayed is one less than what you would expect.
- Close the Output panel and the SWF file window.
Modify your script
You'll modify your script to compensate for the zero indexing.
- Add +1 to the value when you create currentMonth, and test your document to be sure the expected month number appears.
That line of script should read as follows:
var currentMonth:Number = myDate.getMonth()+1;
- Comment the trace statement:
// trace (currentMonth);
- Below the trace statement, set the autoSize property of your text box to true:
currentDate_txt.autoSize = true;
- Use the text property of your text box to display today's date in the form Today is mm/dd/yyyy.
Use the currentMonth variable you already created, plus the geTDate() and getFullYear() methods of the Date object:
currentDate_txt.text="Today is "+currentMonth+"/"+ myDate.getDate() + "/"+myDate.getFullYear();
- Verify that your script appears as follows:
var myDate:Date=new Date(); var currentMonth:Number = myDate.getMonth()+1; // trace (currentMonth); currentDate_txt.autoSize = true; currentDate_txt.text="Today is "+currentMonth+"/"+myDate.getDate() + "/"+myDate.getFullYear();
- Save and test the document.
The current date should appear in the SWF file window.
by
updated