Sunday, 25 May 2008

Display time

This Flash tutorial will teach you how to display the time in Actionscript 2.0. The time function is taken directly from the system clock on your machine.

UPDATE: new Actionscript 3.0 version now available here.

Display Time

Step 1

Create a new Flash document.
Using the dynamic text tool drag a rectangle shapes on the stage like below:




Step 2

Using the selecting tool (v) select the dynamic text box. And then give it a variable name. eg. theTime.




Step 3

Convert the dynamic text box into a movie clip by selecting F8, choose an appropriate name and click ok.


Step 4

Now right click on your movie clip and select Actions.

Add the following code:

onClipEvent (enterFrame) {
myTime = new Date();
theSeconds = myTime.getSeconds();
theMinutes = myTime.getMinutes();
theHours = myTime.getHours();

if (theHours>=12) {

ampm = "pm";

} else {
ampm = "am";
}

if (theHours>=13) {
nHours = nHours-12;
}

if (length(theMinutes) == 1) {
theMinutes = "0" +theMinutes;
}
if (length(theSeconds) == 1) {
theSeconds = "0"+theSeconds;
}

theTime = theHours+":"+theMinutes+":"+theSeconds+" " +ampm;
}


Step 5

Test your movie Ctrl + Enter.



You should now have a time display.

2 comments:

Anmol 23 March 2009 11:12  

gr8 work...

Ste Williams 19 November 2013 09:47  

Hi there,

I have a question regarding a project I'm doing. I need to use this method to display the time plus X minutes...so that in effect the time displays a slightly fast clock.

eg. if the time is actually 3:00pm I need it to display 3:15pm.

The problem I'm having by simply adding a number to theMinutes is that when it reaches a high enough number, but adding the additional numbers it displays an incorrect time...

eg. 3:62pm.

Could anybody help modify this code for me?

Thanks in advance!

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP