-2

Let's Say, I've a two datetime moment date time object.

momentdatefirst = '2017-03-21T05:00:00+05:45'

momentdatesecond = '1990-03-21T07:12:45+05:45'

How do calculate the difference between these two time stamps in moment.js?

I tried this:

var diff_hour = momentdatefirst.diff(momentdatesecond); 

And this doesn't retrieve the actual difference.

user2906838
  • 1,178
  • 9
  • 20
  • 7
    Possible duplicate of [Get the time difference between two datetimes](http://stackoverflow.com/questions/18623783/get-the-time-difference-between-two-datetimes) – m87 Mar 21 '17 at 08:00
  • Sorry, My bug was not because of the diff method but due to the two different date format between first and second date. Sorry – user2906838 Mar 21 '17 at 08:42

2 Answers2

2

You should do it like this:

let date = moment('2017-03-21T05:00:00+05:45');
let dateTwo = moment('1990-03-21T07:12:45+05:45');
let diff = dateTwo.diff(date); // in milliseconds
let diffInHours = date.diff(dateTwo, 'hour'); // in hours
// and so on
console.log('diff in milliseconds', diff);

It will output 852068835000 milliseconds which ~ 27 years.

Dan Cantir
  • 2,915
  • 14
  • 24
2
var a = moment('2016-06-06T21:03:55');//now
var b = moment('2016-05-06T20:03:55');

console.log(a.diff(b, 'minutes')) // 44700
console.log(a.diff(b, 'hours')) // 745
console.log(a.diff(b, 'days')) // 31
console.log(a.diff(b, 'weeks')) // 4

You can go through moment docs here

Bhaurao Birajdar
  • 1,437
  • 11
  • 15