1const date1 = new Date('7/13/2010');
2const date2 = new Date('12/15/2010');
3console.log(getDifferenceInDays(date1, date2));
4console.log(getDifferenceInHours(date1, date2));
5console.log(getDifferenceInMinutes(date1, date2));
6console.log(getDifferenceInSeconds(date1, date2));
7
8function getDifferenceInDays(date1, date2) {
9 const diffInMs = Math.abs(date2 - date1);
10 return diffInMs / (1000 * 60 * 60 * 24);
11}
12
13function getDifferenceInHours(date1, date2) {
14 const diffInMs = Math.abs(date2 - date1);
15 return diffInMs / (1000 * 60 * 60);
16}
17
18function getDifferenceInMinutes(date1, date2) {
19 const diffInMs = Math.abs(date2 - date1);
20 return diffInMs / (1000 * 60);
21}
22
23function getDifferenceInSeconds(date1, date2) {
24 const diffInMs = Math.abs(date2 - date1);
25 return diffInMs / 1000;
26}
1const diffDays = (date, otherDate) => Math.ceil(Math.abs(date - otherDate) / (1000 * 60 * 60 * 24));
2
3// Example
4diffDays(new Date('2014-12-19'), new Date('2020-01-01')); // 1839
1const date1 = new Date('7/13/2010');
2const date2 = new Date('12/15/2010');
3const diffTime = Math.abs(date2 - date1);
4const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
5console.log(diffTime + " milliseconds");
6console.log(diffDays + " days");
1/* difference between date1 and date2 in days (date2 - date1) */
2/* date1 and date 2 are already javascript date objects */
3function dateDifference(date2, date1) {
4 const _MS_PER_DAY = 1000 * 60 * 60 * 24;
5
6 // Discard the time and time-zone information.
7 const utc1 = Date.UTC(date1.getFullYear(), date1.getMonth(), date1.getDate());
8 const utc2 = Date.UTC(date2.getFullYear(), date2.getMonth(), date2.getDate());
9
10 return Math.floor((utc2 - utc1) / _MS_PER_DAY);
11}
1var date1 = new Date();
2 var date2 = new Date("2025/07/30 21:59:00");
3
4showDiff(date1,date2);
5
6function showDiff(date1, date2){
7
8 var diff = (date2 - date1)/1000;
9 diff = Math.abs(Math.floor(diff));
10
11 var days = Math.floor(diff/(24*60*60));
12 var leftSec = diff - days * 24*60*60;
13
14 var hrs = Math.floor(leftSec/(60*60));
15 var leftSec = leftSec - hrs * 60*60;
16
17 var min = Math.floor(leftSec/(60));
18 var leftSec = leftSec - min * 60;
19 console.info("You have " + days + " days " + hrs + " hours " + min + " minutes and " + leftSec + " seconds");
20
21 return {
22 d: days,
23 h: hrs,
24 i: min,
25 s: leftSec
26 };
27}
1var DateDiff = {
2
3 inDays: function(d1, d2) {
4 var t2 = d2.getTime();
5 var t1 = d1.getTime();
6
7 return parseInt((t2-t1)/(24*3600*1000));
8 },
9
10 inWeeks: function(d1, d2) {
11 var t2 = d2.getTime();
12 var t1 = d1.getTime();
13
14 return parseInt((t2-t1)/(24*3600*1000*7));
15 },
16
17 inMonths: function(d1, d2) {
18 var d1Y = d1.getFullYear();
19 var d2Y = d2.getFullYear();
20 var d1M = d1.getMonth();
21 var d2M = d2.getMonth();
22
23 return (d2M+12*d2Y)-(d1M+12*d1Y);
24 },
25
26 inYears: function(d1, d2) {
27 return d2.getFullYear()-d1.getFullYear();
28 }
29}
30
31var dString = "May, 20, 1984";
32
33var d1 = new Date(dString);
34var d2 = new Date();
35
36document.write("<br />Number of <b>days</b> since "+dString+": "+DateDiff.inDays(d1, d2));
37document.write("<br />Number of <b>weeks</b> since "+dString+": "+DateDiff.inWeeks(d1, d2));
38document.write("<br />Number of <b>months</b> since "+dString+": "+DateDiff.inMonths(d1, d2));
39document.write("<br />Number of <b>years</b> since "+dString+": "+DateDiff.inYears(d1, d2));