2

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)
arun kumar
  • 89
  • 1
  • 1
  • 6
  • I'm assuming the `sla` prefix is an issue with copying your code to SO? – George Nov 21 '18 at 13:26
  • 1
    Possible duplicate of [How can I calculate the difference between two times that are in 24 hour format?](https://stackoverflow.com/questions/11038252/how-can-i-calculate-the-difference-between-two-times-that-are-in-24-hour-format) – Gerard Nov 21 '18 at 13:27
  • 2
    That's not a duplicate... the time here is not in 24 hour format – Oram Nov 21 '18 at 13:28
  • First convert both to same time format, may be to IST, then convert to milliseconds and find the difference – Raja Mohamed Nov 21 '18 at 13:30
  • 1
    Possible duplicate of [Get difference between 2 dates in JavaScript?](https://stackoverflow.com/questions/3224834/get-difference-between-2-dates-in-javascript) – adiga Nov 21 '18 at 13:37

6 Answers6

7

You 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.com/

George
  • 6,630
  • 2
  • 29
  • 36
2

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);
Oram
  • 1,589
  • 2
  • 16
  • 22
1

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)
0

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();
Anton Pastukhov
  • 588
  • 2
  • 11
  • 1
    Sure, add a 50 kB library to substract two numbers from each other. Might be a bit of an overkill – Marv Nov 21 '18 at 13:43
  • Sure it is the right way! Initially I wanted to suggest jQuery because of https://i.stack.imgur.com/ssRUr.gif, but then realized it's kinda felt out of fashion – Anton Pastukhov Nov 21 '18 at 13:54
0

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))
-1

Use ms to calculate the difference between two timestamps

const ms = require('ms');

var EndTime = 1541092163000;
var StartTime = 1541077763000;

ms( EndTime - StartTime) //return 4h
Ken Yip
  • 599
  • 1
  • 8
  • 17