Javatpoint Logo
Javatpoint Logo

jQuery get Difference between two Dates in days

In this section, we are going to learn how we can use JQuery to display the number of days between two dates. In our example, we have explained the simple way to get the difference. We will use date and math to get the number of days between dates.

Math() and Date() functions are the JavaScript function, and we are using them to get the difference between two dates in days. That's why we cannot say that we are getting the number of days between dates by using jQuery. By using math and the date function, we cannot only get the difference between dates in days, but we can also get it in months and years also.

In the below example, we will perform the task by creating a daydifference() function, which will contain two arguments, i.e., first date and last date. The difference in days will be returned by this function. In our jQuery application, we have to simply copy that code to get the difference between dates in days. Using the following example, we are able to get the difference. We can also use this code in our jQuery code.

Example:

The approach of the above example is as follows:

  • Firstly, we will define two dates.
  • Then we will use startDay.getTime()- endDay.getTime() to determine the time difference between two dates.
  • After that, we will determine the number of days between dates. We will do this by dividing the number of milliseconds in a day and the time difference of both dates.
  • Lastly, the total number of days between two dates will be returned.

After executing the above code, we will get the following output:

12





Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA