
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Finding Number of Days Between Two Dates JavaScript
We are required to write a JavaScript function that takes in two dates in the 'YYYY-MM-DD' format as the first and second argument respectively. The function should then calculate and return the number of days between the two dates.
For example −
If the input dates are −
const str1 = '2020-05-21'; const str2 = '2020-05-25';
Then the output should be −
const output = 4;
Example
const str2 = '2020-05-25'; const daysBetweenDates = (str1, str2) => { const leapYears = (year, month) => { if (month <= 2){ --year; }; let floor = Math.floor; return floor(year / 400) + floor(year / 4) - floor(year / 100); }; let monthDays = [0, 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30]; for (let i = 1; i < monthDays.length; ++i){ monthDays[i] += monthDays[i - 1]; }; let days = (year, month, d) => (year * 365) + leapYears(year, month) + monthDays[month] + d; let p = days(...str1.split('-').map(Number)); let q = days(...str2.split('-').map(Number)); return Math.abs(p - q); }; console.log(daysBetweenDates(str1, str2));
Output
And the output in the console will be −
4
- Related Articles
- How to get the number of days between two Dates in JavaScript?
- How do I get the number of days between two dates in JavaScript?
- Find number of days between two given dates in C++
- Python program to find number of days between two given dates
- How do I calculate number of days between two dates using Python?
- How to find the number of days and number of weeks between two dates in R?
- How to count days between two dates in Java
- How to get the number of seconds between two Dates in JavaScript?
- Finding the missing number between two arrays of literals in JavaScript
- How to calculate the weeks and days between two dates in Excel?
- How can I calculate full 24hour days between two specified dates in MySQL?
- Find the number of logins between two dates in MySQL
- Finding shared element between two strings - JavaScript
- Finding the difference between two arrays - JavaScript
- What is the way to find business days between two specified dates in MySQL?

Advertisements