get time difference between two dates in minutes javascript code example

Example 1: showing difference between dates in minutes js

const today = new Date();
const endDate = new Date(startDate.setDate(startDate.getDate() + 7));
const days = parseInt((endDate - today) / (1000 * 60 * 60 * 24));
const hours = parseInt(Math.abs(endDate - today) / (1000 * 60 * 60) % 24);
const minutes = parseInt(Math.abs(endDate.getTime() - today.getTime()) / (1000 * 60) % 60);
const seconds = parseInt(Math.abs(endDate.getTime() - today.getTime()) / (1000) % 60);

Example 2: javascript get seconds between two dates

var secondBetweenTwoDate = Math.abs((new Date().getTime() - oldDate.getTime()) / 1000);

Example 3: javascript get hours difference between two dates

// date1 and date2 are date objects

let hours = Math.abs(date1 - date2) / 36e5;

// The subtraction returns the difference between the two dates in milliseconds.
// 36e5 is the scientific notation for 60*60*1000, dividing by which converts
// the milliseconds difference into hours.

Example 4: javascript subtract 2 dates get difference in minutes

var diff = Math.abs(new Date('2011/10/09 12:00') - new Date('2011/10/09 00:00'));