I am in a situation where I need to find out the total hour difference between two date objects but the thing is dates aren’t present in the actual format.
Date 1: 6 Apr, 2015 14:45
Date 2: 7 May, 2015 02:45
If it would have been in standard format, simply I would have been used below method:
var hours = Math.abs(date1 - date2) / 36e5;
I am not sure how do I get the hour difference here… please help.
Advertisement
Answer
You can create date objects out of your strings:
JavaScript
x
8
1
const dateOne = "6 Apr, 2015 14:45";
2
const dateTwo = "7 May, 2015 02:45";
3
const dateOneObj = new Date(dateOne);
4
const dateTwoObj = new Date(dateTwo);
5
const milliseconds = Math.abs(dateTwoObj - dateOneObj);
6
const hours = milliseconds / 36e5;
7
8
console.log(hours);