current time and date, zero before single digits / Actionscript 3.0
Copy link to clipboard
Copied
hello,
i have a problem with a clock that does current date and time. i am newbie and tried all other forums relating to this problem. i tried every possible solution i could find, nothing seemed to work.
can anyone please complete my actionscript 3.0 code?
Thank you a lot in advance,
Kind regards,
Bregt
var today_date:Date = new Date();
var thisday:uint = today_date.getDate();
var thismonth:uint = today_date.getMonth();
var today_time;
var currentTime:Date = new Date();
var minutes = currentTime.getMinutes();
var seconds = currentTime.getSeconds();
var hours = currentTime.getHours() * 30 + currentTime.getMinutes() / 2;
var month:Array = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var day:Array = new Array('zondag','zaterdag','vrijdag','donderdag','woensdag','dinsdag','maandag');
var fileName:String = (today_date.getDate()+month[thismonth]+day[thisday]+today_date.getFullYear()+"_"+currentTime.hours + currentTime.minutes + currentTime.seconds);
trace(fileName);
Copy link to clipboard
Copied
var today_date:Date = new Date();
// you don't need these lines as you're using wrong properties here
/*
var thisday:uint = today_date.getDate(); // wrong
var thismonth:uint = today_date.getMonth();
var today_time;
var currentTime:Date = new Date();
var minutes = currentTime.getMinutes();
var seconds = currentTime.getSeconds();
var hours = currentTime.getHours() * 30 + currentTime.getMinutes() / 2;
*/
// The correct properties:
trace(today_date.date);
trace(today_date.fullYear);
trace(today_date.month);
trace(today_date.day);
trace(today_date.hours);
trace(today_date.minutes);
trace(today_date.seconds);
var month:Array = new Array('January','February','March','April','May','June','July','August','September','Octo ber','November','December');
var day:Array = new Array('zondag','zaterdag','vrijdag','donderdag','woensdag','dinsdag','maandag');
// your code:
/*var fileName:String = (today_date.getDate()+month[thismonth]+day[thisday]+today_date.getFullYear()+"_"+currentTime.hours + currentTime.minutes + currentTime.seconds);
*/
//it's wrong
// You have to select the index of the array element using a number:
// month[today_date.month] // today_date.month is 5
// the fifth element in month() array is "May"...
// So.. month[today_date.month] => month[5] => May
// day[today_date.day] => day[4] => woensdag
// correct code:
var fileName:String = (today_date.date + "_" + month[today_date.month]+ "_" + day[today_date.day]+ "_" + today_date.fullYear + "_" + today_date.hours + "_" + today_date.minutes + "_" + today_date.seconds);
trace(fileName);
// If you want to update the clock at runtime then you have to add a Timer or enter frame event listener:
// Timer Code:
var myTimer:Timer = new Timer(1000) // this timer will call the function every 1000 milliseconds after you add a timer event listener to it.
myTimer.addEventListener(TimerEvent.TIMER, onTimer);
function onTimer(e:TimerEvent):void
{
today_date = new Date();
// convert the "today_date" values to string and store them in "fileName" string:
fileName = String(today_date.date + "_" + month[today_date.month]+ "_" + day[today_date.day]+ "_" + today_date.fullYear + "_" + today_date.hours + "_" + today_date.minutes + "_" + today_date.seconds);
trace(fileName);
};
myTimer.start(); // (it'll not work by itself you have to start it.)
///////// OR ///////
// EnterFrame Code:
/*
addEventListener(Event.ENTER_FRAME, onEF);
function onEF(e:Event):void
{
//update the string
today_date = new Date();
fileName = String(today_date.date + "_" + month[today_date.month]+ "_" + day[today_date.day]+ "_" + today_date.fullYear + "_" + today_date.hours + "_" + today_date.minutes + "_" + today_date.seconds);
trace(fileName);
};
*/
// use one of the above codes
Copy link to clipboard
Copied
if you want to pad numbers to length 2, you can use:
function padF(n:Number):String{
var s:String=n.toString();
while(s.length<2){
s='0'+s;
}
return s;
}
for example:
var d:Date=new Date();
var today=d.fullYear+'/'+month[d.month]+'/'+padF(d.date);
Copy link to clipboard
Copied
thank you a lot,
it works perfectly, but when i test the movie i don't see the clock, it's dynamic text and the type is embedded in the movie and the color is black but i can't see it? is this a live clock, i mean when i change the time, does it automatically adjust it?
thank you in advance,
the noob
Copy link to clipboard
Copied
thanks a lot, it works great, but i see:
6
2016
4
5
21
14
49
6 Mei dinsdag 2016 21:14:49
can i make it that i just see:
6 Mei dinsdag 2016 21:14:49
thank you in advance,
The noob
Copy link to clipboard
Copied
you'll need to update your display every second, if that's the frequency at which you want to display updates to the date/time.
and i can't determine what code you're using that causes this:
6
2016
4
5
21
14
49
6 Mei dinsdag 2016 21:14:49
to be displayed.
Copy link to clipboard
Copied
this is the entire code:
var today_date:Date = new Date();
trace(today_date.date);
trace(today_date.fullYear);
trace(today_date.month);
trace(today_date.day);
trace(today_date.hours);
trace(today_date.minutes);
trace(today_date.seconds);
function padF(n:Number):String{
var s:String=n.toString();
while(s.length<2){
s='0'+s;
}
return s;
}
var month:Array = new Array('Januari','Februari','Maart','April','Mei','Juni','Juli','Augustus','September','Oktober','November','December');
var day:Array = new Array('zondag','maandag','dinsdag','woensdag','donderdag','vrijdag','zaterdag');
var fileName:String = (today_date.date + " " + month[today_date.month]+ " " + day[today_date.day]+ " " + today_date.fullYear + " " + padF(today_date.hours) + ":" + padF(today_date.minutes) + ":" + padF(today_date.seconds));
trace(fileName);
i can see it in the export screen, but when i test the movie as swf i don't see the text. also what code do i have to put in to get the clock to run 'live'?
i am breaking my mind over it, i just got it working in actionscript 2 when i had to change language, so sorry for the stupid questions, but i'm learning
kind regards
Copy link to clipboard
Copied
there are more efficient ways to do this but for most purposes this is (wasteful) but ok,
var tf:TextField=new TextField();
tf.width =300;
addChild(tf);
tf.x=tf.y=100;
function padF(n:Number):String{
var s:String=n.toString();
while(s.length<2){
s='0'+s;
}
return s;
}
var month:Array = new Array('Januari','Februari','Maart','April','Mei','Juni','Juli','Augustus','September','Ok tober','November','December');
var day:Array = new Array('zondag','maandag','dinsdag','woensdag','donderdag','vrijdag','zaterdag');
var t:Timer=new Timer(1000,0);
t.addEventListener(TimerEvent.TIMER,dateF);
function dateF(e:TimerEvent):void{
var today_date:Date = new Date();
tf.text = (today_date.date + " " + month[today_date.month]+ " " + day[today_date.day]+ " " + today_date.fullYear + " " + padF(today_date.hours) + ":" + padF(today_date.minutes) + ":" + padF(today_date.seconds));
}
Copy link to clipboard
Copied
in the output field i see the info but when i publish i don't see anything.
new adjusted code is:
var today_date:Date = new Date();
trace(today_date.date);
trace(today_date.fullYear);
trace(today_date.month);
trace(today_date.day);
trace(today_date.hours);
trace(today_date.minutes);
trace(today_date.seconds);
function padF(n:Number):String{
var s:String=n.toString();
while(s.length<2){
s='0'+s;
}
return s;
}
var month:Array = new Array('Januari','Februari','Maart','April','Mei','Juni','Juli','Augustus','September','Ok tober','November','December');
var day:Array = new Array('zondag','maandag','dinsdag','woensdag','donderdag','vrijdag','zaterdag');
var fileName:String = (today_date.date + " " + month[today_date.month]+ " " + day[today_date.day]+ " " + today_date.fullYear + " " + padF(today_date.hours) + ":" + padF(today_date.minutes) + ":" + padF(today_date.seconds));
trace(fileName);
var tf:TextField=new TextField();
tf.width =300;
addChild(tf);
tf.x=tf.y=100;
var t:Timer=new Timer(1000,0);
t.addEventListener(TimerEvent.TIMER,dateF);
function dateF(e:TimerEvent):void{
var today_date:Date = new Date();
tf.text = (today_date.date + " " + month[today_date.month]+ " " + day[today_date.day]+ " " + today_date.fullYear + " " + padF(today_date.hours) + ":" + padF(today_date.minutes) + ":" + padF(today_date.seconds));
}
thank you
Copy link to clipboard
Copied
dear,
now i get error:
TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::MovieClip@1c41b42571a9 to flash.text.TextField.
at flash.display::Sprite/constructChildren()
at flash.display::Sprite()
at flash.display::MovieClip()
at kloknieuwecode_fla::MainTimeline()
adjusted code:
var today_date:Date = new Date();
trace(today_date.date);
trace(today_date.fullYear);
trace(today_date.month);
trace(today_date.day);
trace(today_date.hours);
trace(today_date.minutes);
trace(today_date.seconds);
function padF(n:Number):String{
var s:String=n.toString();
while(s.length<2){
s='0'+s;
}
return s;
}
var month:Array = new Array('Januari','Februari','Maart','April','Mei','Juni','Juli','Augustus','September','Ok tober','November','December');
var day:Array = new Array('zondag','maandag','dinsdag','woensdag','donderdag','vrijdag','zaterdag');
var fileName:String = (today_date.date + " " + month[today_date.month]+ " " + day[today_date.day]+ " " + today_date.fullYear + " " + padF(today_date.hours) + ":" + padF(today_date.minutes) + ":" + padF(today_date.seconds));
trace(fileName);
var tf:TextField=new TextField();
tf.width =300;
addChild(tf);
tf.x=tf.y=100;
var t:Timer=new Timer(1000,0);
t.addEventListener(TimerEvent.TIMER,dateF);
function dateF(e:TimerEvent):void{
var today_date:Date = new Date();
tf.text = (today_date.date + " " + month[today_date.month]+ " " + day[today_date.day]+ " " + today_date.fullYear + " " + padF(today_date.hours) + ":" + padF(today_date.minutes) + ":" + padF(today_date.seconds));
}
thanks
Copy link to clipboard
Copied
sorry refered to the wrong instance, this problem is solved but as swf i don't see anything. prtscr added.
Copy link to clipboard
Copied
use:
var today_date:Date = new Date();
function padF(n:Number):String{
var s:String=n.toString();
while(s.length<2){
s='0'+s;
}
return s;
}
var month:Array = new Array('Januari','Februari','Maart','April','Mei','Juni','Juli','Augustus','September','Ok tober','November','December');
var day:Array = new Array('zondag','maandag','dinsdag','woensdag','donderdag','vrijdag','zaterdag');
var tf:TextField=new TextField();
tf.width =300;
addChild(tf);
tf.x=tf.y=100;
var t:Timer=new Timer(1000,0);
t.addEventListener(TimerEvent.TIMER,dateF);
t.start();
function dateF(e:TimerEvent):void{
var today_date:Date = new Date();
tf.text = (today_date.date + " " + month[today_date.month]+ " " + day[today_date.day]+ " " + today_date.fullYear + " " + padF(today_date.hours) + ":" + padF(today_date.minutes) + ":" + padF(today_date.seconds));
}
Copy link to clipboard
Copied
i tried it but it won't work, I think i'm to new to get this right.
i just want a live clock for Digital Sinage purposes, that tells me the day, date and time. and i just can't wrap my head around it.
i wanted to adjust an *.fla from the web and adjust it, but then i wouldn't understand what i was doeing.
So thank you for all your efforts and time,
kind regards,
bregt
Copy link to clipboard
Copied
clock works perfectly but still nothing to see when published as swf
Copy link to clipboard
Copied
copy and paste the code you're using.
Copy link to clipboard
Copied
var today_date:Date = new Date();
function padF(n:Number):String{
var s:String=n.toString();
while(s.length<2){
s='0'+s;
}
return s;
}
var month:Array = new Array('Januari','Februari','Maart','April','Mei','Juni','Juli','Augustus','September','Ok tober','November','December');
var day:Array = new Array('zondag','maandag','dinsdag','woensdag','donderdag','vrijdag','zaterdag');
var fileName:String = (day[today_date.day]+ " " +today_date.date + " " + month[today_date.month]+ " " +today_date.fullYear + " " + padF(today_date.hours) + ":" + padF(today_date.minutes) + ":" + padF(today_date.seconds));
trace(fileName);
var tf:TextField=new TextField();
tf.width =300;
addChild(tf);
tf.x=tf.y=100;
var myTimer:Timer = new Timer(1000) // this timer will call the function every 1000 milliseconds after you add a timer event listener to it.
myTimer.addEventListener(TimerEvent.TIMER, onTimer);
function onTimer(e:TimerEvent):void
{
today_date = new Date();
// convert the "today_date" values to string and store them in "fileName" string:
fileName = String(day[today_date.day]+ " " +today_date.date + " " + month[today_date.month]+ " " +today_date.fullYear + " " + padF(today_date.hours) + ":" + padF(today_date.minutes) + ":" + padF(today_date.seconds));
trace(fileName);
};
myTimer.start();
i made a dynamic textfield called 'tf' as i saw in your code, i don't know if this is correct. type is embedded
Copy link to clipboard
Copied
it should be something like the first clock on this webpage called 'digital-white' but with the date and everything.
How to add a flash clock to an info screen project - Dynamic Info Screen - XemiComputers
Copy link to clipboard
Copied
function onTimer(e:TimerEvent):void
{
today_date = new Date();
// convert the "today_date" values to string and store them in "fileName" string:
fileName = String(day[today_date.day]+ " " +today_date.date + " " + month[today_date.month]+ " " +today_date.fullYear + " " + padF(today_date.hours) + ":" + padF(today_date.minutes) + ":" + padF(today_date.seconds));
//Add this line:
tf.text = fileName;
trace(fileName);
};
Copy link to clipboard
Copied
thanks, now i can see it but it doesn't update every second.
Copy link to clipboard
Copied
Make sure that you've added the line inside the "onTimer{}" function.
Copy link to clipboard
Copied
super, works perfectly. code is quite a bit more complicated than as2. thanks to everybody that helped me.
Copy link to clipboard
Copied
sorry guys for bothering you again,
The clock works great now, but visually it has to look like this:
It has to have a transparent background, when the size is changed it has to be in proportion and the type is BureauGrot.
in AS2 it worked perfectly with symbols but now I really don't know what to do.
Any advice? or should I pay 700 Euro to get this developed/designed?
Kind regards,
Bregt
Copy link to clipboard
Copied
i'll do it for less than 100.
but wait to see if someone else will do it for you for free.
Copy link to clipboard
Copied
that would be amazing because I don't have the money if someone would do this this would be great, I just don't understand AS3. AS2 was much clearer for me. where could i find somebody who would do it for free? it is the only thing i need in AS3.
Kind regards,
Bregt
Copy link to clipboard
Copied
this forum.
some other people have been posting here besides me. sometimes other people will work for free.


-
- 1
- 2