Get day, month and year Difference in JavaScript

In the various scenario, we need to calculate the number of months or number of weeks or number of years, etc. between two dates. We can calculate it using various methods of Dates given in JavaScript. Let’s do it.

Let’s assume we have two dates startDate and endDate. We will perform operations on these two dates in this blog.

let startDate = new Date("2010-03-28");
let endDate = new Date("2020-05-01");

Calculate Number of milliseconds

function getMilliSecDiff(startDate, endDate) {
  let startMilliSec = startDate.getTime();
  let endMilliSec = endDate.getTime();
  return endMilliSec - startMilliSec;
}

In JavaScript, Date has method named getTime(). It return time in millisecond. So we can calculate millisecond difference.

Calculate Number of Seconds

function getSecondDiff(startDate, endDate) {
    let milisecsDiff = getMilliSecDiff(startDate, endDate);
    // 1 sec = 1000ms
    return milisecsDiff/1000
}

Calculate Number of Minutes

As we already calculate the number of seconds. Now we will use the above method to get the difference of second and divide it by 60 as 1 min = 60 sec

function getMinuteDiff(startDate, endDate) {
    let secondDiff = getSecondDiff(startDate, endDate);
    // 1 min = 60sec
    return secondDiff/60
}

Calculate Number of Hours

Now we will use above method to get difference of minutes and divide it by 60 as 1 hour = 60 mins.

function getHourDiff(startDate, endDate) {
    let hourDiff = getMinuteDiff(startDate, endDate);
    // 1 hour = 60 mins
    return hourDiff/60
}

Calculate Number of Days

function getDayDiff(startDate, endDate) {
    let dayDiff = getHourDiff(startDate, endDate);
    // 1 day = 24 hours
    return dayDiff/25
}

Calculate Number of months

To calculate number of month there is different way. We will check each way with there problem and benefits.

1. Time-based

This method will give you a month difference on the basis of the number of days, not the only month. This will give nearly an accurate number of months

function getMonthDiff1(startDate, endDate) {
  let diff = (endDate.getTime() - startDate.getTime()) / 1000;
  // 1 month = (60 * 60 * 24 * 365)/12 hours
  diff /= (60 * 60 * 24 * 365) / 12;
  return Math.floor(diff);
}

The accuracy of this method is less than above method for boundary cases.

function getMonthDiff1(startDate, endDate) {
  let diff = (endDate.getTime() - startDate.getTime()) / 1000;
  // 1 month = 4 week
  // 1 week = 7 days
  // 1 day= (60 * 60 * 24) hours
  diff /= (60 * 60 * 24 * 7 * 4);
  return Math.floor(diff);
}

2. Month-based

This method also good to use to get diff of month.

function getMonthDiff(startDate, endDate) {
  let monthsFromYearDiff = (startDate.getFullYear() - endDate.getFullYear()) * 12;
  let monthsFromMonthDiff = startDate.getMonth() - endDate.getMonth();
  let dayDiff = startDate.getDate() - endDate.getDate();
  if (dayDiff < 0) {
    // means startDate is not a complete month
    return monthsFromYearDiff + monthsFromMonthDiff - 1;
  }
  return monthsFromYearDiff + monthsFromMonthDiff;
}

Calculate the number of months and days

It will give the number of the month nd days like 4 month and 3 days.

function getMonthDiff(startDate, endDate) {
  let monthsFromYearDiff = (startDate.getFullYear() - endDate.getFullYear()) * 12;
  let monthsFromMonthDiff = startDate.getMonth() - endDate.getMonth();
  let dayDiff = startDate.getDate() - endDate.getDate();
  if (dayDiff < 0) {
    // means startDate is not a complete month
    // get last day of month
    var d = new Date(startDate.getFullYear(), startDate.getMonth() + 1, 0);

    var numDays = d.getDate();
    dayDiff += numDays;
    let monthDiff =  monthsFromYearDiff + monthsFromMonthDiff - 1;
    return {months:monthDiff, days:dayDiff }
  }
 let monthDiff = monthsFromYearDiff + monthsFromMonthDiff;
 return {months:monthDiff, days:dayDiff }
}

Calculate Number of years

This method will give year difference on the basis of change of year not the number of the month like 31-12-2019 to 01-01-2020

function yearsDiff(startDate, endDate) {
    let yearsDiff =  endDate.getFullYear() - startDate.getFullYear();
    return yearsDiff;
}

Second way to calculate number of month in total than we can divide it by 12 to get number of year.

function getYearDiff(startDate, endDate) {
  let diff = (endDate.getTime() - startDate.getTime()) / 1000;
  // 1 month = (60 * 60 * 24 * 365)/12 hours
  let totalMonth = diff /= (60 * 60 * 24 * 365) / 12;
  return Math.floor(totalMonth /12);
}

These are a few ways to calculate the different types of differences in dates. Even you can use the external library like moment.js to calculate these things.

Complete Code at github

Leave a Reply