PDA

View Full Version : Displaying Time



kirupa
October 21st, 2001, 03:55 PM
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 :)

Thanks in advance!
Kirupa

thoriphes
October 21st, 2001, 07:12 PM
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;
}
</font></tt>

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.

suprabeener
October 22nd, 2001, 01:28 AM
holy geezus thoripes!!

here's a slightly less verbose way:


onClipEvent(enterFrame){
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 (http://pub40.ezboard.com/fkirupafrm8.showMessage?topicID=1556.topic) 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!!

kirupa
October 22nd, 2001, 07:30 AM
Thanks thoripes and supra! I really appreciate it.

Regards,
Kirupa

thoriphes
October 22nd, 2001, 09:11 PM
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?

suprabeener
October 23rd, 2001, 01:56 AM
did you try it?

eyezberg
October 23rd, 2001, 07:58 AM
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?...

suprabeener
October 23rd, 2001, 02:43 PM
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.

kirupa
October 24th, 2001, 07:24 AM
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 (http://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 :)

Cheers,
Kirupa

Syko
October 26th, 2001, 02:07 PM
I tried that but ...am i dum(i think so) or what ...a blank dynamic textbox appears...
no numbers no nothing...:(
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...

suprabeener
October 26th, 2001, 02:51 PM
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!

upuaut8
October 27th, 2001, 12:28 AM
that's the tightest time code I've ever seen supra.. nicely done.

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

Syko
November 3rd, 2001, 10:54 AM
Cool man, it worked!!!

ilyaslamasse
February 24th, 2002, 03:28 PM
Cool ! Look what I did :
pom 0]

tiger20
March 2nd, 2005, 01:57 PM
This is another way using Day/Month + time

layton
April 6th, 2005, 10:59 PM
Hello, I am unable to open the attached .fla with Flash MX 2004. Is anyone else having this problem? I'd really like to check it out. Thanks.

SAVAGER
January 4th, 2006, 12:28 AM
me too... I'm using flash 8

shadowcaster31
March 5th, 2006, 12:01 PM
how to make a timer countdown instead? Like 15 sec countdown before the turn is over in a multiplayer game (if the player doesnt do anything) for example.

nathan99
December 4th, 2006, 09:14 AM
more updated code:

myDate = new Date();
updateTime = setInterval(function ():Void {
hours = myDate.getHours();
min = myDate.getMinutes();
ampm = hours>12 ? "pm" : "am";
if (hours>12) {
hours -= 12;
}
if (hours == 0) {
hours = 12;
}
now = hours+":"+min+" "+ampm;
}, 30000);

ruan
March 26th, 2007, 04:32 PM
hey I am new at Flash, but here is what I've done:


_root.onEnterFrame = function(){
var myDate = new Date();
var myHours = myDate.getHours();
var myMinutes = myDate.getMinutes();
var AmPm;
if (myHours < 12) {
AmPm = "AM";
}
else {
AmPm = "PM";
}
if (myHours > 12) {
myHours = myHours - "12";
}
if (myHours < 10) {
myHours = "0" + myHours;
}
if (myMinutes < 10) {
myMinutes = "0" + myMinutes;
}
time_txt.text = myHours + ":" + myMinutes + AmPm;
// time_txt is an instance name of your clock, so don't forget to change it to whatever you named it
}

jeckx2
April 11th, 2007, 01:16 PM
thanks for the download tiger ROAR your the best