How to Calculate Days Between Dates

If you run a website on WordPress and often find the need to calculate days between dates, then integrating such a tool into your site can be highly beneficial. One tool that allows you to do this is the WordPress Plugin ‘Calculator Builder’. Here is how you can create a tool that calculates day differences using Calculator Builder.

Step 1: Install and Activate Calculator Builder Plugin

  • Navigate to Plugins > Add New in your WordPress dashboard.
  • Search for Calculator Builder.
  • Click ‘Install Now’ and then ‘Activate’.

Step 2: Add a New Calculator

  • Go to the Calculator Builder section and select Calculator > Add New.
  • Name it (e.g., “Days Difference Calculator”).

Step 3: Design the Interface

  • Add fields: “Start Date” and “End Date” using Input > Date.
  • Include a “Result Box” to show the computed days.

Step 4: Add Javascript Code in block ‘Formula’

Now, you need to add custom Javascript code to calculate the difference between the start and end dates.

Next JavaScript code calculates the number of days between two dates defined by x[1] and x[2].


const date1 = new Date(x[1]);
const date2 = new Date(x[2]);
const days = daysBetween(date1, date2);
y[1] = days;

function daysBetween(date1, date2) {
  const date1Ms = date1.getTime();
  const date2Ms = date2.getTime();

  const differenceMs = Math.abs(date1Ms - date2Ms);

  const days = Math.floor(differenceMs / (1000 * 60 * 60 * 24));

  return days;
}

Here is a breakdown of the code:

  1. const date1 = new Date(x[1]); and const date2 = new Date(x[2]);: These two lines create two new Date objects date1 and date2 from array x at indices 1 and 2 respectively. The new Date() constructor creates a new date object from a specified date.
  2. const days = daysBetween(date1, date2);: This line calculates the number of days between date1 and date2 by calling the daysBetween() function, and storing the result in the days constant.
  3. y[1] = days;: This line assigns the calculated number of days between date1 and date2 to the array y at index 1.

The daysBetween() function takes two Date objects as parameters and calculates the number of days between them:

  1. const date1Ms = date1.getTime(); and const date2Ms = date2.getTime();: These two lines get the time in milliseconds since the Unix Epoch (Jan 01, 1970) of date1 and date2 respectively.
  2. const differenceMs = Math.abs(date1Ms - date2Ms);: This line calculates the absolute difference between date1 and date2 in milliseconds.
  3. const days = Math.floor(differenceMs / (1000 * 60 * 60 * 24));: This line converts the milliseconds difference to days. There are 1000 milliseconds in a second, 60 seconds in a minute, 60 minutes in an hour, and 24 hours in a day.
  4. return days;: Finally, the function returns the calculated number of days.

The overall goal of the given JavaScript code is to calculate the number of days between two dates and assign that value to the second position of array y[1].

Step 6: Publish the Calculator

Once you’ve finished configuring the calculator, click ‚Save‘. The calculator is now ready to be used on any WordPress page or post. Just copy the shortcode of the calculator and paste it where you want it to be displayed on your website’s front end.

Conclusion

In conclusion, the Calculator Builder for WordPress offers an intuitive platform for building custom calculators, including ones for calculating days between dates. This makes it easy for website users to quickly and easily calculate date differences without leaving the webpage. It also requires minimal coding knowledge, making it accessible even to those new to web development.

View our live calculator here