Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between dates in JavaScript

How to find the difference between two dates?

like image 395
Saranya Avatar asked Dec 28 '09 06:12

Saranya


People also ask

How do I calculate the difference between two dates in TypeScript?

To calculate the time between 2 dates in TypeScript, call the getTime() method on both dates when subtracting, e.g. date2. getTime() - date1. getTime() . The getTime() method returns a number representing the milliseconds between the unix epoch an the given date.


2 Answers

I have found this and it works fine for me:

Calculating the Difference between Two Known Dates

Unfortunately, calculating a date interval such as days, weeks, or months between two known dates is not as easy because you can't just add Date objects together. In order to use a Date object in any sort of calculation, we must first retrieve the Date's internal millisecond value, which is stored as a large integer. The function to do that is Date.getTime(). Once both Dates have been converted, subtracting the later one from the earlier one returns the difference in milliseconds. The desired interval can then be determined by dividing that number by the corresponding number of milliseconds. For instance, to obtain the number of days for a given number of milliseconds, we would divide by 86,400,000, the number of milliseconds in a day (1000 x 60 seconds x 60 minutes x 24 hours):

Date.daysBetween = function( date1, date2 ) {   //Get 1 day in milliseconds   var one_day=1000*60*60*24;    // Convert both dates to milliseconds   var date1_ms = date1.getTime();   var date2_ms = date2.getTime();    // Calculate the difference in milliseconds   var difference_ms = date2_ms - date1_ms;    // Convert back to days and return   return Math.round(difference_ms/one_day);  }  //Set the two dates var y2k  = new Date(2000, 0, 1);  var Jan1st2010 = new Date(y2k.getFullYear() + 10, y2k.getMonth(), y2k.getDate()); var today= new Date(); //displays 726 console.log( 'Days since '             + Jan1st2010.toLocaleDateString() + ': '             + Date.daysBetween(Jan1st2010, today)); 

The rounding is optional, depending on whether you want partial days or not.

Reference

like image 34
Adrian P. Avatar answered Sep 19 '22 14:09

Adrian P.


By using the Date object and its milliseconds value, differences can be calculated:

var a = new Date(); // Current date now. var b = new Date(2010, 0, 1, 0, 0, 0, 0); // Start of 2010. var d = (b-a); // Difference in milliseconds. 

You can get the number of seconds (as a integer/whole number) by dividing the milliseconds by 1000 to convert it to seconds then converting the result to an integer (this removes the fractional part representing the milliseconds):

var seconds = parseInt((b-a)/1000); 

You could then get whole minutes by dividing seconds by 60 and converting it to an integer, then hours by dividing minutes by 60 and converting it to an integer, then longer time units in the same way. From this, a function to get the maximum whole amount of a time unit in the value of a lower unit and the remainder lower unit can be created:

function get_whole_values(base_value, time_fractions) {     time_data = [base_value];     for (i = 0; i < time_fractions.length; i++) {         time_data.push(parseInt(time_data[i]/time_fractions[i]));         time_data[i] = time_data[i] % time_fractions[i];     }; return time_data; }; // Input parameters below: base value of 72000 milliseconds, time fractions are // 1000 (amount of milliseconds in a second) and 60 (amount of seconds in a minute).  console.log(get_whole_values(72000, [1000, 60])); // -> [0,12,1] # 0 whole milliseconds, 12 whole seconds, 1 whole minute. 

If you're wondering what the input parameters provided above for the second Date object are, see their names below:

new Date(<year>, <month>, <day>, <hours>, <minutes>, <seconds>, <milliseconds>); 

As noted in the comments of this solution, you don't necessarily need to provide all these values unless they're necessary for the date you wish to represent.

like image 103
Sampson Avatar answered Sep 22 '22 14:09

Sampson