Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

current time and date, zero before single digits / Actionscript 3.0

Explorer ,
May 04, 2016 May 04, 2016

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);

TOPICS
ActionScript
2.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 04, 2016 May 04, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 05, 2016 May 05, 2016

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);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 06, 2016 May 06, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 06, 2016 May 06, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 06, 2016 May 06, 2016

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 06, 2016 May 06, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 06, 2016 May 06, 2016

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));

}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 06, 2016 May 06, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 06, 2016 May 06, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 06, 2016 May 06, 2016

sorry refered to the wrong instance, this problem is solved but as swf i don't see anything. prtscr added.Schermafbeelding 2016-05-06 om 22.45.09.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 06, 2016 May 06, 2016

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));

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 06, 2016 May 06, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 06, 2016 May 06, 2016

clock works perfectly but still nothing to see when published as swf

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 06, 2016 May 06, 2016

copy and paste the code you're using.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 06, 2016 May 06, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 06, 2016 May 06, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 06, 2016 May 06, 2016

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);

};

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 06, 2016 May 06, 2016

thanks, now i can see it but it doesn't update every second.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 06, 2016 May 06, 2016

Make sure that you've added the line inside the "onTimer{}" function.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 06, 2016 May 06, 2016

super, works perfectly. code is quite a bit more complicated than as2. thanks to everybody that helped me.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 08, 2016 May 08, 2016

sorry guys for bothering you again,

The clock works great now, but visually it has to look like this:

klok + datum ontwerp.jpg

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 08, 2016 May 08, 2016

i'll do it for less than 100.

but wait to see if someone else will do it for you for free.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 08, 2016 May 08, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 08, 2016 May 08, 2016

this forum.

some other people have been posting here besides me.  sometimes other people will work for free.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines