Get the number of days between two dates using jQuery

Today, we’ll explain to you how to get the number of days between two dates using jQuery.

Sometimes, we need to calculate the number of days between two dates. Using getTime() function, we can easily do this task.

Here, we will create the HTML form where we will take two input fields with datepicker and one button to calculate the number of days.

Write the following code and run to check output.

index.html

How it works

  1. Get the value of two dates.
  2. Initialize them by creating the date objects using new Date().
  3. Calculate the time difference of two dates using dt2.getTime() - dt1.getTime().
  4. Calculate the no. of days by dividing total milliseconds (1000 * 60 * 60 * 24).
  5. Print the result in div with id result.

Output

Output - Get the number of days between two dates using jQuery - Clue Mediator
Output – Get the number of days between two dates using jQuery – Clue Mediator

That’s it for today.
Thank you for reading. Happy Coding..!! 🙂

If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *