Tabs left
Home
Scripts
Tutorials
Forums
Info Left
Info Right
Content Gradient Left
Top Scripts and Tutorials!
Scripts:
Simple JavaScript Clock
Rating: 3/5
A simple javascript clock!

FlatFile Hits Counter
Rating: 3/5
Use this script to see how many people have visite

Tutorials:
Using the date function in ActionScript 3.0
Rating: 3/5
How to use the date function in ActionScript 3

PHP loops
Rating: 3/5
What are PHP loops, and how to create them.

Get Firefox
Ubuntu - Snazzy Valid XHTML 1.0 Transitional
Valid CSS!
Using the date function in ActionScript 3.0

Files

 time.fla
 date.fla

Instructions | Troubleshooting

Difficulty: Easy
Time it takes to complete: 30 minutes
Rating: 3/5 (238 total votes)            (Rate this tutorial!)

Instructions:

The date function can we used to make a lot of different things, such as the countdown we had up on this site before it was released. In this tutorial, I will show you the different parts of the date function. All of the tutorial files will be availiable for download to the left.

WARNING: You will need to keep in mind that ActionScript is case sensitive, so you need to include the caps where I have them!

First off, you will need to make a new 'Flash File (ActionScript 3.0)'. For now, click on the new layer icon, as shown in the first image of the image gallery at the top.

Now double-click on the new layer's name (should be 'Layer 2') and name it 'actions'. This is where we will do all of our actionscript. Now, hit F9 (or option + F9 if you are running a Mac).

Since you will be using the date function more than once, you need to set it as a variable. To do this, type the following:

var date:Date = new Date();

This keeps date information inside the date variable. We will be able to access it to get all sorts of useful information on the date and time.

Since this is the date function, we will deal with all things to do with the date. This function is useful because it gets the data from the user's computer. You can get the current day, year, week and month using the function. It is pretty self-explanatory how you get the different things, since the lovely people over at Adobe didn't use anything stupid to get you to call the different variables.

When you are calling anything to do with the date and time, you will need to use the following syntax:

date.getSomethingHere();

You replace the getSomethingHere() with whatever you want to get. This is a table of the different things you can get using the date function:

Replace with What is does
getFullYear() Gets the full year, in 4 digit format
getDate() Gets the day of the month, like 23.
getDay() Gets the current day in the form of Monday, Tuesday etc.
getMonth() Gets the current month in numbers. WARNING: 0 is January and 11 is December!

For instance, if we wanted to get the date in a format like: Monday 21 January 2008 we would do:

var date:Date = new Date();

var year = date.getFullYear();
var month = date.getMonth();
var dayNo = date.getDate();
var day = date.getDay();

var myDate = day + " " + dayNo + " " + month + " " + year;

myDate.toString();

trace(myDate);

Then, in the output box, the current date will be outputted like: 2 27 4 2008. However, we want to make it with words as well. For this you will need to create an array like the following:

var months:Array = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

That one is for the months. You only need to do one for the months, and one for the days of the week like so:

var days:Array = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");

But with arrays on their own, they won't set the correct variable, so you need to add these to correct it and give us the output we want:

month = months[month];
day = days[day];

My source code now looks like this, and yours should be similar:

var date:Date = new Date();

var year = date.getFullYear();
var month = date.getMonth();
var dayNo = date.getDate();
var day = date.getDay();

var months:Array = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
month = months[month];

var days:Array = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
day = days[day];

var myDate = day + " " + dayNo + " " + month + " " + year;

myDate.toString();

trace(myDate);

If you test the movie now, you will get: Tuesday 27 May 2008 (well, I did, since that is the day today)

Getting the time is pretty much the same, so for this I will just give you another table with all of the different functions in and an example.

You need to do the same thing as if you were getting a date variable, by doing:

date.getSomethingHere();

Replacing the getSomethingHere() with one of the things below:

Replace with What is does
getHours() Gets the current hours, on the 24 hour clock like 23 or 18
getMinutes() Gets the current minutes, like 45 or 38
getSeconds() Gets the current seconds, such as 34 or 0
getMilliseconds() Gets the current milliseconds, which range from 0 to 999
getTime(); Gets the current UNIX timestamp, which is

A working example with these is:

// Set Date variable

var date:Date = new Date();

// Get the variables for time

var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();

// Glue it together

var time = hours + ":" + minutes + ":" + seconds;

// Stringify it

time.toString();

// Trace it

trace(time);

Thanks for reading and good luck with all things flash! If you need any help, check out the forums. I will be posting another tutorial soon on how you can use the things you have learnt in here, to make a simple digital clock which updates itself every second!

Troubleshooting:

By: samwilliamh

^^ Back to top ^^

Custom Search
Content Gradient Right