Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
92
rated 0 times [  93] [ 1]  / answers: 1 / hits: 15739  / 9 Years ago, mon, april 13, 2015, 12:00:00

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.


More From » javascript

 Answers
316

You can create date objects out of your strings:





const dateOne = 6 Apr, 2015 14:45;
const dateTwo = 7 May, 2015 02:45;
const dateOneObj = new Date(dateOne);
const dateTwoObj = new Date(dateTwo);
const milliseconds = Math.abs(dateTwoObj - dateOneObj);
const hours = milliseconds / 36e5;

console.log(hours);




[#67095] Friday, April 10, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jamilab

Total Points: 687
Total Questions: 88
Total Answers: 86

Location: Antigua and Barbuda
Member since Sat, Apr 24, 2021
3 Years ago
jamilab questions
;