Days Between Dates: Formula and Calculator
The calculator for this page is switched off while it is checked. The formula is below, and the worked example in the article shows how to use it by hand.
This calculator works out the number of days between two dates. You enter a start date and an end date, and it returns the exact day count, including or excluding the end date as you choose. The calculation is straightforward: subtract the start date from the end date, then convert the difference from milliseconds to days.
Formula
The basic formula for days between two dates is:
Days = (End Date – Start Date) / (1000 × 60 × 60 × 24)
Here, the difference between the two dates is measured in milliseconds (the way JavaScript and many programming languages store dates). Dividing by 86,400,000 (the number of milliseconds in one day) converts that difference into whole days. If you want to include the end date in the count, add 1 to the result.
Each variable is:
- Start Date: the earlier date, usually entered as YYYY-MM-DD.
- End Date: the later date, also in YYYY-MM-DD format.
- Days: the number of 24-hour periods between the two dates.
This formula assumes both dates are in the same time zone and that daylight saving time changes are ignored. For most everyday uses, that is perfectly accurate.
Worked Example
Using the default values from the calculator above:
- Start Date: January 1, 2024
- End Date: December 31, 2024
- Include end date? No
Step 1: Convert both dates to milliseconds since January 1, 1970 (the Unix epoch).
January 1, 2024 is 1,704,067,200,000 ms. December 31, 2024 is 1,735,296,000,000 ms.
Step 2: Subtract the start from the end.
1,735,296,000,000 – 1,704,067,200,000 = 31,228,800,000 ms
Step 3: Divide by 86,400,000 ms per day.
31,228,800,000 / 86,400,000 = 365 days
Since “include end date� is set to No, the result is 365. If you set it to Yes, it becomes 366. That matches the actual number of days in 2024, which was a leap year.
When to Use This Calculation
You might need the days-between formula for:
- Calculating how long a project has been running or how many days until a deadline.
- Determining the length of a lease, contract, or warranty period.
- Counting days for billing, interest, or payroll (though financial calculations often use specific day-count conventions).
- Tracking personal milestones like anniversaries or age in days.
- Planning events or trips by knowing exactly how many days are in between.
The formula works for any two dates, past or future, as long as you enter them in a consistent format.
How to Read the Result
The result is a whole number representing full 24-hour periods. For example, if you enter January 1 and January 2, the difference is 1 day. If you enter January 1 and January 1, the difference is 0 days.
When “include end date� is checked, the calculator adds 1 to the difference. This is common when counting inclusive ranges, such as “from Monday to Friday� meaning 5 days (Mon, Tue, Wed, Thu, Fri), not 4.
If the end date is earlier than the start date, the result will be negative. Some applications prefer an absolute value, but this calculator returns the signed difference so you can see the direction of the span.
Common Mistakes and Limitations
One frequent error is mixing date formats. Always use YYYY-MM-DD to avoid ambiguity, especially when working with international dates. Another mistake is forgetting that the formula counts midnight-to-midnight, not partial days. If you need to account for hours, you must include time in the date values.
Daylight saving time can cause issues if you are comparing dates across a DST boundary. In many regions, a day is 23 or 25 hours long on those transition days. The formula here ignores DST, which is fine for most purposes but can be off by one hour if you are calculating precise durations.
Leap years are handled automatically because the formula works with the actual calendar. The year 2024 has 366 days, so the difference between Jan 1 and Dec 31 is 366 if you include the end date, or 365 if not.
Alternatives to the Formula
If you are not writing code, you can use:
- Spreadsheet functions like DAYS() in Excel or Google Sheets, which use the same subtraction principle.
- Online date calculators, which often allow you to add or subtract days from a date, not just find the difference.
- Programming languages: in JavaScript,
Dateobjects can be subtracted directly; in Python, thedatetimemodule does this natively.
For financial calculations, be aware that some contexts use 30/360 day-count conventions, where every month is treated as 30 days. The formula here uses the actual calendar, which is more accurate for general use.
Frequently Asked Questions
How do I count days excluding weekends?
To exclude weekends, you would need to iterate through each day and check the day of the week, or use a formula that accounts for the number of full weeks and the remainder. This calculator does not do that, but many spreadsheet functions like NETWORKDAYS() can.
What if the start date is after the end date?
The formula will return a negative number. If you want a positive count, you can take the absolute value, or simply swap the dates. Some calculators automatically reverse the order, but this one returns the signed difference.
Does the formula include the start date?
No, the difference is measured from the start date to the end date. The start date is not counted. If you want to include both the start and end dates, you add 1 to the result, which is what the “include end date� option does.
Why does my manual calculation sometimes differ by one day?
This usually happens because of time zone differences or because you are counting inclusively. For example, if you count “from Jan 1 to Jan 2� as two days (including both), you get 2, but the raw difference is 1. Also, if you are in a time zone with DST, a day might not be exactly 24 hours.
Can I use this formula for dates before 1970?
Yes, the formula works for any dates that your system can represent. JavaScript, for instance, supports dates from about 100 million days before and after 1970. For very old dates, you may need to use a library that handles historical calendars.
Is there a difference between calendar days and business days?
Yes. Calendar days include weekends and holidays. Business days typically exclude weekends and sometimes holidays. This calculator gives calendar days. To get business days, you would need to filter out Saturdays, Sundays, and any holidays you define.
Related
Formula used
in start | Start date | - | 2024-01-01 | 0 in end | End date | - | 2024-12-31 | 0 in include | Include end date? | No=0, Yes=1 | 0 out days | Days between | days | (end - start) / 86400000 + include | 0
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.