我有两个时间戳,我需要两个小时之间的区别。如何计算它们。
var resolution
var EndTime = 1541092163000
var StartTime = 1541077763000
resolution = slaEndTime - slaStartTime
var resolutionTime = ((resolution / (1000 * 60)) % 60)
答案 0 :(得分:2)
您只需要除法而不是时间和模量
/1000
会将其转换为秒数
第一个/60
将转换为分钟
以及最近/60
小时
var resolution
var EndTime = 1541092163000
var StartTime = 1541077763000
resolution = EndTime - StartTime
var resolutionTime = (((resolution / 1000) / 60)/ 60)
console.log(resolutionTime)
或者您可以使用momentjs https://momentjs.com/
答案 1 :(得分:0)
请参见下面的代码段。 您需要先除以1000,再除以60,然后再除以60,得到小时。 简而言之,您需要除以1000 * 60 * 60 = 3600000。
var endTime = 1541092163000;
var startTime = 1541077763000;
var differenceInMiliseconds = endTime - startTime;
var differenceInSeconds = differenceInMiliseconds / 1000;
var differenceInMinutes = differenceInSeconds / 60;
var differenceInHours = differenceInMinutes / 60;
console.log(differenceInHours);
// or in short
console.log((endTime - startTime) / 3600000);
答案 2 :(得分:0)
您尝试这样做吗?
var resolution
var EndTime = 1541092163000
var StartTime = 1541077763000
var resolution = EndTime - StartTime
var resolutionTime = (parseFloat(resolution) / (60000*60) )
console.log(resolutionTime)
答案 3 :(得分:0)
从手动日期操作混乱中解脱出来,并使用moment.js
const moment = require('moment');
var EndTime = 1541092163000
var StartTime = 1541077763000
var resolution = moment(EndTime - StartTime).asHours();
答案 4 :(得分:0)
这种方法有助于获得基本的小时,分钟,秒故障。
const Duration = (difference) => {
let secondsInMiliseconds = 1000,
minutesInMiliseconds = 60 * secondsInMiliseconds,
hoursInMiliseconds = 60 * minutesInMiliseconds;
var differenceInHours = difference / hoursInMiliseconds,
differenceInMinutes = differenceInHours % 1 * 60,
differenceInSeconds = differenceInMinutes % 1 * 60;
return {
"hours" : Math.floor(differenceInHours),
"minutes" : Math.floor(differenceInMinutes),
"seconds" : Math.floor(differenceInSeconds)
}
}
let aLittleWhileAgo = (new Date()-10000000)
let now = new Date();
console.log(Duration(now-aLittleWhileAgo))
答案 5 :(得分:-1)
使用ms来计算两个时间戳之间的差异
const ms = require('ms');
var EndTime = 1541092163000;
var StartTime = 1541077763000;
ms( EndTime - StartTime) //return 4h