- Parse the dates into date objects.
var dateA = new Date("2009-10-11");
var dateB = new Date("2011-10-09");
- Subtracting the 2 date objects will result in the difference in microseconds.
var diff = Math.abs(dateA - dateB);
- Finally, just calculate the difference accordingly. For example, 1 day has
24 hrs X 60 mins X 60 secs X 1000 ms = 86400000 ms.
var diffDays = Math.floor(diff / 86400000);
That covers the basics, but read on for more examples!
ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.
REAL QUICK SLIDES
TABLE OF CONTENTS
|Download & Notes||Date Difference||Useful Bits & Links|
DOWNLOAD & NOTES
Firstly, here is the download link to the example code as promised.
EXAMPLE CODE DOWNLOAD
Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.
1) DIFFERENCE IN HOURS, DAYS, WEEKS
// (A) PARSE INTO DATE OBJECTS var dateA = new Date("2012-11-10"); var dateB = new Date("2011-10-09"); // (B) DIFFERENCE IN MICROSECONDS var diff = Math.abs(dateA - dateB); console.log("Microseconds: " + diff); // (C) DIFFERENCE IN SECONDS diff = Math.floor(diff / 1000); console.log("Seconds: " + diff); // (D) DIFFERENCE IN HOURS // 1 HR = 60 MINS X 60 SECS = 3600 SECS var diffhrs = Math.floor(diff / 3600); console.log("Hours: " + diffhrs); // (E) DIFFERENCE IN DAYS // 1 DAY = 24 HRS X 60 MINS X 60 SECS = 86400 SECS var diffdays = Math.floor(diff / 86400); console.log("Days: " + diffdays); // (F) DIFFERENCE IN WEEKS // 7 DAYS = 604800 SECS var diffweeks = Math.floor(diff / 604800); console.log("Weeks: " + diffweeks);
var dateA = new Date("2012-11-10");
var dateB = new Date("2011-10-09");
var unixA = dateA.getTime();
var unixB = dateB.getTime();
So far so good? Let’s get back to the example – We can then use the Unix Timestamps to calculate the difference between 2 dates.
var diff = Math.abs(unixA - unixB); // MICROSECONDS
diff = Math.floor(diff / 1000); // SECONDS
Yep, this is the “long-form”. As you see from the above example, we can directly subtract the date objects themselves
Math.abs(dateA - dateB) to get the difference in microseconds. The rest should be pretty straightforward –
- One hour has 3600 seconds. So to get the difference in hours, we do a simple divide
Math.floor(diff / 3600).
- One day has 86400 seconds, to get the difference in days –
Math.floor(diff / 86400)
- And so on…
2) DIFFERENCE IN MONTHS
// (A) DATES var dateA = new Date("2011-01-10"); var dateB = new Date("2010-08-09"); // (B) DIFFERENCE IN YEARS var diffYears = Math.abs(dateA.getFullYear() - dateB.getFullYear()); // (C) DIFFERENCE IN MONTHS var diffMonths = (12 * diffYears) + (dateA.getMonth() - dateB.getMonth());
Some of you sharp code ninjas should have noticed by now – One month is not exactly 30 days, the above method of dividing the seconds is not going to be accurate. A little bit more painful in this case, but we can still get the difference by calculating with the formula
(12 X ABSOLUTE(DIFFERENCE-IN-YEARS)) + (MONTH-A - MONTH-B).
USEFUL BITS & LINKS
That’s all for the main tutorial, and here is a small section on some extras and links that may be useful to you.
INFOGRAPHIC CHEAT SHEET
LINKS & REFERENCES
Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!