9951 explained code solutions for 126 technologies


nodejsHow to get difference between 2 dates in hours


const date1 = new Date("08/05/2022 23:41:20");
const date2 = new Date("08/06/2022 02:56:32");

const diff = date2.getTime() - date1.getTime();
const hours = Math.ceil(diff / (1000 * 60*60));ctrl + c
new Date(

creates new object to work with dates based on given date/time string

const diff

difference in timestamp between 2 timestamps

diff / (1000 * 60*60)

convert milliseconds to hours

hours

will contain number of hours between date1 and date2