1

I have this function:

dateDifference: function(start_date, end_date)
{
    var date1 = new Date(start_date);
    var date2 = new Date(end_date);
    var timeDiff = Math.abs(date2.getTime() - date1.getTime());
    return timeDiff;
}

how you can see I calculate the difference between two dates passed as parameter, now the end result is like this:

55000

But I want the result in minutes how I can achieve this?

Sandokan
  • 1,075
  • 3
  • 15
  • 30

2 Answers2

6

You got milliseconds so you can divide them by 1000 and 60 and get result in minutes.

dateDifference: function(start_date, end_date)
{
    var date1 = new Date(start_date);
    var date2 = new Date(end_date);
    var timeDiff = Math.abs((date2.getTime() - date1.getTime()) / 1000 / 60);
    return timeDiff;
}
Ruben Nagoga
  • 2,178
  • 1
  • 19
  • 30
2

to get from 55000 to seconds, divide by 1000.

then divide by 60 to get minutes.

like so:-

function dateDifference(start_date, end_date)
{
    var date1 = new Date(start_date);
    var date2 = new Date(end_date);
    var milSeconds = Math.abs(date2.getTime() - date1.getTime());
    
    var seconds = milSeconds / 1000;
  
    var minutes = seconds / 60;
  
    return minutes;
}

console.log(dateDifference('01/12/2016 09:00:00', '01/12/2016 10:00:00')); // 60 minutes
BenG
  • 14,826
  • 5
  • 45
  • 60
  • 1
    You may want to throw a parseInt in there somewhere – mplungjan Jan 21 '16 at 15:31
  • @mplungjan—where? *getTime* returns a Number, and both division and subtraction coerce to number too, so belt and braces already. ;-) Far worse is parsing strings with the Date constructor… – RobG Jan 22 '16 at 01:27
  • Don't parse strings with the Date constructor, the format used here is known to be treated differently in different browsers. Always manually parse strings, use a library if you must but a suitable parsing function that will work in every browser that supports scripting is 2 lines of code. See [*Javascript - Find seconds between two MM-dd-yyyy HH:mm:ss formatted dates?*](http://stackoverflow.com/questions/34844761/javascript-find-seconds-between-two-mm-dd-yyyy-hhmmss-formatted-dates/34847066#34847066). – RobG Jan 22 '16 at 01:29
  • Any division may yield a decimal number hence the parseInt. I disagree on @RobG's comment for known and supported formats of which there are many – mplungjan Jan 22 '16 at 05:17
  • @mplungjan—the OP does not specify integer minuets or any rounding. Parsing of the format in the answer by either the Date constructor and Date.parse is not supported by ECMA-262 (which is the only specification that counts here) nor all browsers in use, even the most modern ones. – RobG Jan 22 '16 at 06:35
  • For this specific format,Windows: Chrome: OK, Fx: OK, IE: OK. iOS: Safari OK, Chrome OK: http://dygraphs.com/date-formats.html – mplungjan Jan 22 '16 at 07:54
  • @mplungjan—the point is that a 2 line function to parse the string is guaranteed to work in every ECMA-262 implementation, regardless of version, in the past, present and future (i.e. the holy grail of cross–browser and future–proof). The format in the OP isn't guaranteed to work for any of them, it must be tested in every browser in use (which must be 30 or 40 at least) and every new browser as it's released. Which approach is less work in the long term? – RobG Jan 25 '16 at 00:44
  • If the date is to be user supplied they should be mover be allowed to enter a date. If not the date should be on a defined format. I am not disputing that a known base must be found. So let's by all means split the string :) – mplungjan Jan 25 '16 at 05:09