Posted By

adrianparr on 07/07/11


Tagged

time format timer digital clock formatted hours minutes as3 hrs mins


Versions (?)

AS3 Simple digital clock


 / Published in: ActionScript 3
 

This example expects there to be a dynamic textfield on the stage with the instance name of 'myTextFieldOnStage'. Remember to embed the fonts (Numerals and Colon).

  1. import flash.text.TextField;
  2.  
  3. var timeTextfield:TextField = myTextFieldOnStage;
  4. timeTextfield.text = getFormattedTime();
  5.  
  6. var clockTimer:Timer = new Timer(1000, 0);
  7. clockTimer.addEventListener(TimerEvent.TIMER, onClockTimer);
  8. clockTimer.start();
  9.  
  10. function onClockTimer(e:TimerEvent):void {
  11. timeTextfield.text = getFormattedTime();
  12. }
  13.  
  14. function getFormattedTime():String {
  15. var now:Date = new Date();
  16. var hrs:String = String(now.getHours());
  17. if (hrs.length < 2) {
  18. hrs = "0" + hrs;
  19. }
  20. var mins:String = String(now.getMinutes());
  21. if (mins.length < 2) {
  22. mins = "0" + mins;
  23. }
  24. return hrs + ":" + mins;
  25. }

Report this snippet  

You need to login to post a comment.