avatarJavascript Jeep🚙💨

Summary

The provided web content offers a tutorial on calculating the time difference between two dates in JavaScript, covering seconds, minutes, hours, days, weeks, months, and years.

Abstract

The article is a comprehensive guide aimed at developers who wish to calculate the time difference between two dates in JavaScript. It begins by demonstrating the creation of two date objects, d1 and d2, using Date.now() and new Date(). The tutorial then proceeds to detail methods for finding the difference in various units of time, including seconds, minutes, hours, days, weeks, months, and years. Each section introduces a function that performs the calculation by converting milliseconds to the desired time unit, using arithmetic operations and JavaScript's built-in Date methods. The article emphasizes the importance of understanding the inconsistency of month lengths when calculating the number of months between dates, and it concludes with a call to action for readers to follow the author and consider making a donation to support those in need of food.

Opinions

  • The author believes that understanding how to calculate time differences is an important skill for JavaScript developers.
  • The article suggests that using built-in JavaScript methods, such as getFullYear() and getMonth(), can simplify the calculation of years and months between dates.
  • The author indicates that not all months have the same number of days, which necessitates a specific approach when calculating the difference in months.
  • The author expresses gratitude to the readers and encourages them to provide feedback or corrections through private notes.
  • There is an appeal to the reader's sense of community and altruism, with the author pledging to donate 80% of any contributions received to help provide food for those in need.
  • The author invites readers to follow their work, suggesting that the reader may find value in their continued contributions on the topic of JavaScript.

Find the difference between Dates in JavaScript

Learn how to find difference between two dates in JavaScript.

Image from Lukas Blazek

We learn about finding Number of Seconds , Number of Minutes , Number of hours , Number of days , Number of weeks , Number of months , Number of years between two dates.

Let’s create two dates

let d1 = Date.now();
let d2 = new Date(2019,5,22).getTime(); //Jun 22 2019 in millisecond
console.log(d1); //1573527098946
console.log(d2); //1561141800000

Number of Seconds between two dates.

1 second = 1000 milliseconds 

Now we are having two date d1 and d2 in milliseconds. To convert milliseconds into seconds ,we can divide the difference between two dates in milliseconds 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

Now we have method to find the number of seconds between two dates , so we can find seconds difference, then divide that by 60 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

Now 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 

Now 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

Now 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 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(June → 5) 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;
}

Thanks for Reading 📖 . Hope you like this. If you found any typo or mistake send me a private note 📝 thanks 🙏 😊 .

Follow me JavaScript Jeep🚙💨 .

Please make a donation here. 80% of your donation is donated to someone needs food 🥘. Thanks in advance.

JavaScript
Javascript Tips
Programming
Js
Web Development
Recommended from ReadMedium