I have two timestamps and I need difference between them in hours. How to calculate them.
var resolution
var EndTime = 1541092163000
var StartTime = 1541077763000
resolution = slaEndTime - slaStartTime
var resolutionTime = ((resolution / (1000 * 60)) % 60)
I have two timestamps and I need difference between them in hours. How to calculate them.
var resolution
var EndTime = 1541092163000
var StartTime = 1541077763000
resolution = slaEndTime - slaStartTime
var resolutionTime = ((resolution / (1000 * 60)) % 60)
Share
Improve this question
edited Nov 22, 2018 at 10:58
arun kumar
asked Nov 21, 2018 at 13:24
arun kumararun kumar
991 gold badge1 silver badge6 bronze badges
5
-
I'm assuming the
sla
prefix is an issue with copying your code to SO? – George Commented Nov 21, 2018 at 13:26 - 1 Possible duplicate of How can I calculate the difference between two times that are in 24 hour format? – Gerard Commented Nov 21, 2018 at 13:27
- 2 That's not a duplicate... the time here is not in 24 hour format – Oram Commented Nov 21, 2018 at 13:28
- First convert both to same time format, may be to IST, then convert to milliseconds and find the difference – Raja Mohamed Commented Nov 21, 2018 at 13:30
- 1 Possible duplicate of Get difference between 2 dates in JavaScript? – adiga Commented Nov 21, 2018 at 13:37
6 Answers
Reset to default 8You just need to divide rather than do times and modulus
/1000
will convert it to seconds
the first /60
will convert to minutes
and the last /60
hours
var resolution
var EndTime = 1541092163000
var StartTime = 1541077763000
resolution = EndTime - StartTime
var resolutionTime = (((resolution / 1000) / 60)/ 60)
console.log(resolutionTime)
Or you can use momentjs https://momentjs./
See the snippet below. You need to divide by 1000 then by 60 and then by 60 again to get the hours. In short you need do divide by 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);
have you tried to do this ?
var resolution
var EndTime = 1541092163000
var StartTime = 1541077763000
var resolution = EndTime - StartTime
var resolutionTime = (parseFloat(resolution) / (60000*60) )
console.log(resolutionTime)
Save yourself from manual dates manipulation insanity and use moment.js
const moment = require('moment');
var EndTime = 1541092163000
var StartTime = 1541077763000
var resolution = moment(EndTime - StartTime).asHours();
This approach helped to get a basic hours, minutes, seconds breakdown.
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))
Use ms to calculate the difference between two timestamps
const ms = require('ms');
var EndTime = 1541092163000;
var StartTime = 1541077763000;
ms( EndTime - StartTime) //return 4h