I am trying to calculate time difference given a start datetime and end datetime. I have 2 date objects to calculate the time difference. The issue is when the dates span across a DST day (eg:- Nov 7th).
start datetime = Nov 6th, 16:30
end datetime = Nov 7th, 01:00
Expected difference = 8.5 hours, but received 9.5 hours
Using Luxon JS Library
<script src="https://cdn.jsdelivr.net/npm/luxon@1.25.0/build/global/luxon.min.js"></script>
<script>
startDate = luxon.DateTime.fromObject({year: 2021, month: 11, day: 06, hour: 16, minute: 30 }, { zone: 'America/New_York' });
endDate = luxon.DateTime.fromObject({year: 2021, month: 11, day: 07, hour: 01, minute: 0 }, { zone: 'America/New_York' });
diff = Math.abs(startDate.diff(endDate).as('hours'));
console.log(startDate.toMillis() + ' ' + endDate.toMillis() + ' ' + diff)
</script>
The above code gives me 570 which is 9.5 hours. I am expecting 510 which is 8.5 hours using the diff function.
I can use the offset on each of these dates to plus the offset difference
offset=(endDate.offset - startDate.offset)/60
console.log(endDate.diff(startDate, 'hours').plus({hours: offset}).toObject())
I wanted to know if there is another way to solve this using the diff method.