我正在使用javascript对象,试图将毫秒转换为它的小时、分钟和秒数。
我的currentTime以毫秒为单位
var currentTime = new Date().getTime()我有futureTime,以毫秒计
var futureTime = '1432342800000'我想得到毫秒的差
var timeDiff = futureTime - currentTimetimeDiff是
timeDiff = '2568370873'我想知道它是多少个小时,几分钟,几秒。
有人能帮忙吗?
发布于 2015-04-23 07:46:44
const secDiff = timeDiff / 1000; //in s
const minDiff = timeDiff / 60 / 1000; //in minutes
const hDiff = timeDiff / 3600 / 1000; //in hours 更新
function msToHMS( ms ) {
// 1- Convert to seconds:
let seconds = ms / 1000;
// 2- Extract hours:
const hours = parseInt( seconds / 3600 ); // 3,600 seconds in 1 hour
seconds = seconds % 3600; // seconds remaining after extracting hours
// 3- Extract minutes:
const minutes = parseInt( seconds / 60 ); // 60 seconds in 1 minute
// 4- Keep only seconds not extracted to minutes:
seconds = seconds % 60;
alert( hours+":"+minutes+":"+seconds);
}
const timespan = 2568370873;
msToHMS( timespan ); 演示
发布于 2019-10-23 22:19:05
如果你确信这段时间总是少于一天,你可以用这条线:
new Date(timeDiff).toISOString().slice(11,19) // HH:MM:SS如果timeDiff 超过一天,这将是错误的。
发布于 2020-01-28 12:34:58
将ms转换为hh:mm:ss
function millisecondsToHuman(ms) {
const seconds = Math.floor((ms / 1000) % 60);
const minutes = Math.floor((ms / 1000 / 60) % 60);
const hours = Math.floor((ms / 1000 / 3600 ) % 24)
const humanized = [
pad(hours.toString(), 2),
pad(minutes.toString(), 2),
pad(seconds.toString(), 2),
].join(':');
return humanized;
}
=https://stackoverflow.com/questions/29816872
复制相似问题