Skip to main content
Participating Frequently
May 5, 2016
Question

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

  • May 5, 2016
  • 2 replies
  • 3067 views

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

This topic has been closed for replies.

2 replies

kglad
Community Expert
Community Expert
May 5, 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);

Participating Frequently
May 6, 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

Inspiring
May 5, 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