javascript
Javascript to get the number of days between two dates
Here, we are going to explain the methods and functions that can be used to get the number of days between two given dates.
const date_a = new Date("09/18/2021");
const date_b = new Date("10/20/2021");
const delta = date_b.getTime() - date_a.getTime();
const num_of_days = delta / (1000 * 60 * 60 * 24);
console.log("Number of days: " + num_of_days);
const date_1 = '2021-02-06';
const date_2 = '2021-03-19';
const num_of_days = moment(date_2).diff(moment(date_1), 'days');
If you are using the moment.js in your project then you can use it to calculate the number of days between two dates. The above code example shows the functionality where we are using the moment() function and the diff() function to find the number of days between the dates.
Was this helpful?
Similar Posts
- Lodash - Get random number between two numbers
- Replace multiple spaces between words with single space using javascript
- Get number of keys in an object Javascript
- Combine or merge two or multiple arrays into single array in Javascript
- Swapping two values using Destructuring in Javascript
- Join two arrays and remove duplicate elements using Javascript
- Javascript program to swap two numbers without using temporary variable