Minute difference between two dates

Asked

Viewed 112 times

0

I’m trying to catch the minute difference between two Javascript dates

Javascript:

function compare() {
            var currentDate = new Date().format("DD/MM/YYYY HH:mm");
            var timeStamp = currentDate.getTime();

            var oldDate = new Date().format("DD/MM/YYYY HH:mm");
            var timeStamp2 = oldDate.getTime();

            var dif = timeStamp - timeStamp2 ;

        }

what I would have to change for my function to work properly ?

  • Related: https://answall.com/q/398391/112052

1 answer

2


function diff(datetime2, datetime1) {
  const miliseconds = datetime2.getTime() - datetime1.getTime();
  const seconds = miliseconds / 1000;
  const minutes = seconds / 60;

  return Math.abs(Math.round(minutes));
}

const oldDate = new Date(2019, 06, 19, 13, 40, 00); // 19-07-2019 13:40:00
const currentDate = new Date(); // Data e horário atual

console.log(diff(currentDate, oldDate));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.