Displaying Time

Hi everyone,
I have a quick question on time in Flash. I have messed around with the Date() function, but it is providing more information than i really need. How would I go about displaying the current time in the format: “hh/mm PM or AM”?

I’ve tried numerous methods of displaying time, and for some reason, I’m getting error messages. I’ve also checked out page 440-441 of Colin Moock’s ASDG :slight_smile:

Thanks in advance!

yeah, for some reason, the Date() object is only a one shot thing. You need to continually create new Date() objects to correctly view seconds/hours/minutes/etc… So here’s my code to correctly display the time:
<font color=blue><tt>
onClipEvent (load) {
&nbsp &nbsp &nbsp &nbsp x = 0;
onClipEvent (enterFrame) {
&nbsp &nbsp &nbsp &nbsp _root[“t”+x] = new Date();
&nbsp &nbsp &nbsp &nbsp if (_root[“t”+x].getHours()>12) {
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp hour = _root[“t”+x].getHours()-12;
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp ampm = “PM”;
&nbsp &nbsp &nbsp &nbsp } else if (_root[“t”+x].getHours()<1) {
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp hour = 12;
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp ampm = “AM”;
&nbsp &nbsp &nbsp &nbsp } else {
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp hour = _root[“t”+x].getHours();
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp if (hour == 12) {
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp ampm = “PM”;
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp } else {
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp ampm = “AM”;
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp }
&nbsp &nbsp &nbsp &nbsp }
&nbsp &nbsp &nbsp &nbsp if (_root[“t”+x].getMinutes()<10) {
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp minute = “0”+_root[“t”+x].getMinutes();
&nbsp &nbsp &nbsp &nbsp } else {
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp minute = _root[“t”+x].getMinutes();
&nbsp &nbsp &nbsp &nbsp }
&nbsp &nbsp &nbsp &nbsp if (_root[“t”+x].getSeconds() < 10) {
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp second = “0”+_root[“t”+x].getSeconds();
&nbsp &nbsp &nbsp &nbsp } else {
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp second = _root[“t”+x].getSeconds();
&nbsp &nbsp &nbsp &nbsp }
&nbsp &nbsp &nbsp &nbsp this.time = hour+":"+minute+" “+second+” "+ampm;
&nbsp &nbsp &nbsp &nbsp ++x;

you put this in an MC with a dynamic text called “time” (variable name)

this code works, so if you’re still having problems…contact me.

holy geezus thoripes!!

here’s a slightly less verbose way:

        myDate = new Date();
_ _ _ _ hours = myDate.getHours();
_ _ _ _ if(hours>12){hours-=12;ampm="pm";}
_ _ _ _ else{if(hours==0){hours=12;}ampm="am";}
_ _ _ _ min = myDate.getMinutes();
_ _ _ _ now = hours+":"+min+" "+ampm;

you’ll need a dynamic text box named “now”.

i’ve been editing this constantly as i find errors, i think it’s good now. those embeded if statements are a little messy … but it’s not so bad. ; )

ok last edit, this post has the same, but with an (pseudo) analogue clock, featuring a sweep second hand, and a display that shows the milliseconds (necessary for the sweeping).

c’est tout!!

Thanks thoripes and supra! I really appreciate it.


hm…supra, will that really work? it seems i have to create new date objects to constantly get the time, is this code just to display the time once?

did you try it?

supra, maybe you forgot to mention this needs a two frame-loop to be updated, or am i wrong? your code in the 1st, and go previous in the next, i’d figure?..

it’s in an onClipEvent(enterFrame), so it executes all the time.

it’s not necessary for a movie to be playing to trigger an onClipEvent(enterFrame), so it works even in a one frame movie.

In case any of you have been wondering what I have been up to with the time in Flash, you can check out what I am currently working on: www.kirupa.com/xp/aboutme_xp3.html

The animation is far from being done, but one day it will be! I should be getting my copy of Windows XP in a few days, then I can use real-life ‘realism’ instead of relying on screenshots from across the Web.

You can press reload to see a different XP background image display. Also, I will have an icon on the desktop crediting Microsoft for the design inspiration :slight_smile:


I tried that but …am i dum(i think so) or what …a blank dynamic textbox appears…
no numbers no nothing…:frowning:
I dunno if i have done something wrong …but… it wont work i…mean nothing happens… i think its logical too and it should work but… nothing no errors no nothing …just a blank dynamic textbox…

here’s how a little more explicitly:

make a dynamic text box named “now”.

make that text box a movie (select it and press F8).

select the movie you’ve just created (it may already be selected), and copy the code in my earlier post into the actions window.

that should do it!

that’s the tightest time code I’ve ever seen supra… nicely done.

onClipEvent(enterFrame){} Learn it, love it, be it…

Cool man, it worked!!!

Cool ! Look what I did :
pom 0]