Find the difference between Dates in JavaScript

June 27, 2020

You Learn about about finding difference in Seconds, Minutes, hours, days, weeks, months, years between two dates

Let’s create two variables d1 which points today, and d2 which denotes same day in previous year in millisecond

let d1 = Date.now();    // current day & time in millisecond

//
let today =  new Date(d1);
let previousYear = today.getFullYear() - 1;
today.setYear( previousYear );

let d2 = today.getTime(); // current day and time of previousYear in millisecond

Number of Seconds between two dates

1 second = 1000 milliseconds

Now you are having two date d1 and d2 in milliseconds. To convert milliseconds into seconds ,we can divide the difference between of d1 and d2 by 1000

function secondsDiff(d1, d2) {
   let millisecondDiff = d2 - d1;
   let secDiff = Math.floor( ( d2 - d1) / 1000 );
   return secDiff;
}

Number of Minutes between two dates

1 minutes = 60 seconds

We have method to find the number of seconds between two dates , so we can find seconds difference, then divide that by 60 which will give us the number of minutes between two dates

function minutesDiff(d1, d2) {
     let seconds = secondsDiff(d1, d2);
     let minutesDiff = Math.floor( seconds / 60 );
     return minutesDiff;
}

Number of Hours between two dates

1 hour = 60 minutes

We have method to find the number of minutes between two dates , so we can find minutes difference, then divide that by 60 will give us the number of hours between two dates

function hoursDiff(d1, d2) {
   let minutes = minutesDiff(d1, d2);
   let hoursDiff = Math.floor( minutes / 60 );
   return hoursDiff;
}

Number of Days between two dates

1 day = 24 hours

We have method to find the number of hours between two dates , so we can find difference, then divide that by 24 will give us the number of days between two dates

function daysDiff(d1, d2) {
   let hours = hoursDiff(d1, d2);
   let daysDiff = Math.floor( hours / 24 );
   return daysDiff;
}

Number of Weeks between two dates

1 week = 7 days

We have method to find the number of days between two dates , so we can find difference, then divide that by 7 will give us the number of weeks between two dates

function weeksDiff(d1, d2) {
   let days = daysDiff(d1, d2);
   let weeksDiff = Math.floor( days/ 7 );
   return weeksDiff;
}

Number of Years between two dates

To find number of years between two dates we have in-built method getFullYear , Subtract the date2 year with date1 year, we will get yearsDiff

function yearsDiff(d1, d2) {
    let date1 = new Date(d1);
    let date2 = new Date(d2);
    let yearsDiff =  date2.getFullYear() - date1.getFullYear();
    return yearsDiff;
}

Number of Months between two dates

1 month = 30||31||27||28 days

Number of days in month is not same in all months , so we need to do it differently

Steps :

  • First we need to find number of years between two dates.
  • Multiply number of years between two dates by 12(because for each year 12 months)
  • Subtract the month number(Jan → 0, Feb → 1, ...) of second date with the month number of first date
  • Finding number of months between two dates
function monthsDiff(d1, d2) {
  let date1 = new Date(d1);
  let date2 = new Date(d2);
  let years = yearsDiff(d1, d2);
  let months =(years * 12) + (date2.getMonth() - date1.getMonth()) ;
  return months;
}