Method 1: Use getTime() method
This method involves converting the ZoneDateTime objects to Unix timestamps using the getTime() method and then subtracting them to get the time difference in milliseconds:
var date1 = new Date('2021-05-15T10:00:00.000Z'); var date2 = new Date('2021-05-15T11:30:00.000Z'); var diff = Math.abs(date2.getTime() - date1.getTime()); console.log(diff); //Output: 5400000 (milliseconds)
Method 2: Use moment.js library
var date1 = moment('2021-05-15T10:00:00.000Z'); var date2 = moment('2021-05-15T11:30:00.000Z'); var diff = date2.diff(date1, 'milliseconds'); console.log(diff); //Output: 5400000 (milliseconds)
Using moment.js has the advantage of being more straightforward and providing more options for formatting and manipulating dates and times.