我正在创建一个简单的倒数计时器,其中正在从文本文件中读取日期。日期是纳秒格式。当计算两个日期之间的差异时,结果显示为NaN
。
我尝试了trace(isNaN(currenttime))
和trace(isNaN(targettime))
,他们都返回false。然而,当我在timeleft上做同样的事情时,我得到了错误的含义,这意味着它不是一个有效的日期格式。这是我的代码。
提前感谢您的帮助。
//Loads variables from external file called data.txt
//yr2016 - April 18 2016 - 1460952000000
//yr2017 - April 18 2017 - 1492488000000
//yr2018 - April 17 2018 - 1523976000000
//yr2019 - April 15 2019 - 1555300800000
//yr2020 - April 15 2020 - 1586923200000
//yr2021 - April 15 2021 - 1618459200000
//yr2022 - April 15 2022 - 1649995200000
this.onEnterFrame = function() {
loadvariablesnum("data.txt",0);
var today:Date = new Date();
var currentyear = today.getFullYear();
var currenttime = today.getTime();
switch(currentyear) {
case 2016:
var targetdate:Date = new Date(yr2016);
var targettime = targetdate.getTime();
break;
case 2017:
var targetdate:Date = new Date(yr2017);
var targettime = targetdate.getTime();
break;
case 2018:
var targetdate:Date = new Date(yr2018);
var targettime = targetdate.getTime();
break;
case 2019:
var targetdate:Date = new Date(yr2019);
var targettime = targetdate.getTime();
break;
case 2020:
var targetdate:Date = new Date(yr2020);
var targettime = targetdate.getTime();
break;
case 2021:
var targetdate:Date = new Date(yr2021);
var targettime = targetdate.getTime();
break;
case 2022:
var targetdate:Date = new Date(yr2022);
var targettime = targetdate.getTime();
break;
}
var timeLeft = targetTime - currentTime;
var sec = Math.floor(timeLeft/1000);
var min = Math.floor(sec/60);
var hrs = Math.floor(min/60);
var days = Math.floor(hrs/24);
sec = String (sec % 60);
if (sec.length < 2) {
sec = "0" + sec;
}
min = String (min % 60);
if (min.length < 2) {
min = "0" + min;
}
hrs = String (hrs % 24);
if (hrs.length < 2) {
hrs = "0" + hrs;
}
days = String(days);
var counter:String = days + ":" + hrs + ":" + min + ":" + sec;
time_txt.text = counter;
}