我用AS3编写了这个函数,它将毫秒转换为可读的时间格式:00:00:00 (hh/mm/ss)。
function convertTime(millis:Number):String {
var Seconds = ((millis / 1000) % 60);
var Minutes = (((millis / 1000) / 60) % 60);
var Hours = ((((millis / 1000) / 60) / 60) % 24);
if ( Math.floor(Seconds) < 10 ) {
var newSeconds = "0" + Math.floor(Seconds);
} else {
var newSeconds = Math.floor(Seconds);
}
if ( Math.floor(Minutes) < 10 ) {
var newMinutes = "0" + Math.floor(Minutes);
} else {
var newMinutes = Math.floor(Minutes);
}
if ( Math.floor(Hours) < 10 ) {
var newHours = "0" + Math.floor(Hours);
} else {
var newHours = Math.floor(Hours);
}
return (newHours + ":" + newMinutes + ":" + newSeconds);
}
这一切似乎都工作,除了秒只返回一个单位数,我肯定是相关的以下内容:
flash编译器抛出"警告3596:重复变量定义"在else语句中设置的变量的每个实例?
我做错了吗?
这些在条件语句中的事实应该意味着每个变量只设置一次,对吗?
或者我必须在AS3中明确删除else(s)吗?如:
if ( Math.floor(Hours) < 10 ) {
var newHours = "0" + Math.floor(Hours);
}
if ( Math.floor(Hours) >= 10 ) {
var newHours = Math.floor(Hours);
}
啊,我找到了。AS3对var的声明是非常严格的。
var已经在if语句中设置好了,所以没有必要在else语句中重新声明它:
if ( Math.floor(Hours) < 10 ) {
var newHours = "0" + Math.floor(Hours);
} else {
newHours = Math.floor(Hours);
}
[EDIT]
为了避免所有这些if和else(假设使用get Timer),您可以使用Date类代替,并使用flash.globalization.DateTimeFormatter类。
要格式化输出,只需使用setDateTimePattern()方法。在您的例子中,使用setDateTimePattern("hh:mm:ss");除非你想检索毫秒数…
DateTimeFormatter类是非常有用的,但似乎有一个问题,当你试图获得毫秒("hh:mm:ss:SSS"),所以我试图改进代码,以获得毫秒,如果你需要它。
[编辑2]import flash.utils.getTimer;
import flash.globalization.DateTimeFormatter;
import flash.globalization.DateTimeStyle;
var currentTime = new Date();
function getMS():String{
var ms = currentTime.milliseconds;
if (ms<10)
{
ms = "000" + ms;
}
if (ms<100)
{
ms = "00" + ms;
}
return ms.toString();
}
function formatDate(date:Date) {
var dtf:DateTimeFormatter = new DateTimeFormatter("en,EN");
dtf.setDateTimePattern("yyyy-MM-dd HH:mm:ss:");
var longDate:String = dtf.format(date);
trace(longDate.toString() + getMS());
//trace("***LocaleID requested=" + dtf.requestedLocaleIDName);
//trace("***Format requested (" + dtf.getDateTimePattern() + ")");
}
trace("setDateTimePattern example");
formatDate(currentTime);
// output the current time formated as "hh:mm:ss:SSS"
在这个例子中输出是:
setDateTimePattern example
2016-10-23 11:53:32:979
[/编辑2](/编辑)致以最亲切的问候。尼古拉斯。
正如您所发现的,这是由于AS3处理变量声明的方式。它源于AS3范围模型,使用功能级范围,但没有块级范围。代码中的变量声明将提升到到函数的顶部,相当于:
function convertTime(millis:Number):String {
var Seconds = ((millis / 1000) % 60);
var Minutes = (((millis / 1000) / 60) % 60);
var Hours = ((((millis / 1000) / 60) / 60) % 24);
var newSeconds; //Seconds 'if' case
var newSeconds; //Seconds 'else' case
var newMinutes; //Minutes 'if' case
var newMinutes; //Minutes 'else' case
var newHours; //Hours 'if' case
var newHours; //Hours 'else' case
if ( Math.floor(Seconds) < 10 ) {
newSeconds = "0" + Math.floor(Seconds);
} else {
newSeconds = Math.floor(Seconds);
}
if ( Math.floor(Minutes) < 10 ) {
newMinutes = "0" + Math.floor(Minutes);
} else {
newMinutes = Math.floor(Minutes);
}
if ( Math.floor(Hours) < 10 ) {
newHours = "0" + Math.floor(Hours);
} else {
newHours = Math.floor(Hours);
}
return (newHours + ":" + newMinutes + ":" + newSeconds);
}
当然你会看到警告3596