function differenceInMilliseconds(date1, date2) {
const { getTime: getTime1 } = new Date(date1);
const { getTime: getTime2 } = new Date(date2);
return getTime1() - getTime2();
}
differenceInMilliseconds('2021-01-01', '2021-01-02');
function differenceInMilliseconds(date1, date2) {
const t1 = new Date(date1).getTime();
const t2 = new Date(date2).getTime();
return t1 - t2;
}
differenceInMilliseconds('2021-01-01', '2021-01-02');
In JavaScript, class methods are not direct properties of an instance, but rather belong to the class’s prototype. When you try to destructure a method, you’re attempting to extract it directly from the instance, which doesn’t work because getTime isn’t a direct property. On the other hand, new Date().getTime() works because JavaScript checks the prototype chain and finds getTime on the Date prototype.