在用echarts绘制动态折线图 + 时间轴时,要求展示最新20秒内的数据,但是出现了在最新一分钟开始的前十秒渲染失败,经检查发现,时间轴的时间要求必须是两位数,即当时/分/秒小于10时,必须在前面加一个“0”,否则就会出现前十秒渲染失败。
附:获取时间戳对应时间函数(精确到毫秒)
getNowTime(timeStamp, milliSeconds?: number) {
let date = new Date(timeStamp);
let year = date.getFullYear();
let month = date.getMonth() + 1 > 9 ? date.getMonth() + 1 : "0" + date.getMonth() + 1;
let day = date.getDate() > 9 ? date.getDate() : "0" + date.getDate();
let hour = date.getHours() > 9 ? date.getHours() : "0" + date.getHours();
let minute =
date.getMinutes() > 9 ? date.getMinutes() : "0" + date.getMinutes();
let second =
date.getSeconds() > 9 ? date.getSeconds() : "0" + date.getSeconds();
let currentTime = ""
if(milliSeconds) {
currentTime = year + "/" + month + "/" + day + " " + hour + ":" + minute + ":" +
second + "." + milliSeconds;
} else {
currentTime = year + "/" + month + "/" + day + " " + hour + ":" + minute + ":" +
second;
}
return currentTime;
}