0

我想用 actionscript 在 flash 中创建一个计时计数器。分钟/秒显示良好,但问题是小时以 1 而不是 0 开头。代码如下:

import flash.utils.Timer;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.globalization.DateTimeFormatter;

var timer:Timer = new Timer(100);
timer.start();
timer.addEventListener(TimerEvent.TIMER, timerTickHandler);
var timerCount:int = 0;

function timerTickHandler(Event:TimerEvent):void
{
    timerCount += 100;
    toTimeCode(timerCount);
}

function toTimeCode(milliseconds:int) : void {
    //create a date object using the elapsed milliseconds
    var time:Date = new Date(milliseconds);

    //define minutes/seconds/mseconds
    var hours:String = String(time.hours);
    var minutes:String = String(time.minutes);
    var seconds:String = String(time.seconds);


    //add zero if neccecary, for example: 2:3.5 becomes 02:03.5
    hours = (hours.length != 2) ? '0'+hours : hours;
    minutes = (minutes.length != 2) ? '0'+minutes : minutes;
    seconds = (seconds.length != 2) ? '0'+seconds : seconds;




    //display elapsed time on in a textfield on stage
    timer_txt.text = hours + ":" + minutes + ":" + seconds;

}

我尝试了所有方法,但计数器开始时仍然有小时 = 1 !!!

4

1 回答 1

0

我认为您可能会因为Date自动处理夏令时而获得一个小时的偏移量。尝试改用 UTC 值:

var hours:String = String(time.hoursUTC);
var minutes:String = String(time.minutesUTC);
var seconds:String = String(time.secondsUTC);
于 2013-03-24T10:24:52.000 回答