'Time difference between two time showing wrong values in js

I want to check time difference between two times and get difference in minutes, using javascript and my time format is 12 hrs with am/pm

for example :

compare minutes difference between (10:35 am) -(01:15 pm)= ? minutes

But the problem is I am getting wrong values for this, so how to calculate minutes between two time using javascript

<script>

var timeStart = new Date("01/23/2020 " + "05:00 AM");
var timeEnd = new Date("01/23/2020 " + "06:30 PM");

var diff = (timeEnd - timeStart) / 60000; 

var minutes = diff % 60;
var hours = (diff - minutes) / 60;
alert(minutes);
alert(hours);

</script>


Solution 1:[1]

This Could be the Short and Sweet solution

var diff = Math.abs(new Date('01/23/2020 06:30 PM') - new Date('01/23/2020 05:00 AM'));
var minutes = Math.floor((diff/1000)/60);
alert(minutes);

Solution 2:[2]

Total minutes will be calculated as

var timeStart = new Date("01/23/2020 " + "05:00 AM");
var timeEnd = new Date("01/23/2020 " + "06:30 PM");

var diff = (timeEnd - timeStart) / 60000; 

var minutes = diff % 60;
var hours = (diff - minutes) / 60;
var totalMinutes = (hours*60)+minutes;
alert(totalMinutes);

Solution 3:[3]

this will give approximate minute diff in utc.

let startTime = new Date("01/23/2020 " + "05:00 AM");
let endTime = new Date("01/23/2020 " + "06:30 PM");

let diffInMinutes = (timeEnd - timeStart) / 60000;
let minutes = Math.floor(diff / 60);

to get the local time zone's minute difference you have to add the timezone offset when calculation the diff. you can do so by ->

let diffInMinutesWithTZOffset = diff + new Date().getTimezoneOffset();

where diffInMinutesWithTZOffset will produce correct difference according to each timezone.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Punit Gajjar
Solution 2 Farhan
Solution 3 Gourab Paul