本文介绍了如何将毫秒转换为“hhmmss”?使用javascript格式化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用javascript Date对象尝试将毫秒转换为多少小时,分钟和秒。

I am using javascript Date object trying to convert millisecond to how many hour, minute and second it is.

我有以毫秒为单位的currentTime

I have the currentTime in milliseconds

var currentTime = new Date().getTime()

我有futureTime(毫秒)

and I have futureTime in milliseconds

var futureTime = '1432342800000'

我希望得到毫秒差异

var timeDiff = futureTime - currentTime

timeDiff

timeDiff = '2568370873'

我想知道它是多少小时,分钟,秒。

I want to know how many hours, minutes, seconds it is.

有人可以帮忙吗?

推荐答案

var secDiff = timeDiff / 1000; //in s
var minDiff = timeDiff / 60 / 1000; //in minutes
var hDiff = timeDiff / 3600 / 1000; //in hours  

更新

function msToHMS( ms ) {
    // 1- Convert to seconds:
    var seconds = ms / 1000;
    // 2- Extract hours:
    var hours = parseInt( seconds / 3600 ); // 3,600 seconds in 1 hour
    seconds = seconds % 3600; // seconds remaining after extracting hours
    // 3- Extract minutes:
    var minutes = parseInt( seconds / 60 ); // 60 seconds in 1 minute
    // 4- Keep only seconds not extracted to minutes:
    seconds = seconds % 60;
    alert( hours+":"+minutes+":"+seconds);
}

var timespan = 2568370873; 
msToHMS( timespan );  

Demo

这篇关于如何将毫秒转换为“hhmmss”?使用javascript格式化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 12:46