Date Calculator
Calculate the difference between dates or add/subtract time periods.
Counting the days between two dates
Enter both dates and read the difference. The question that has to be settled before the number means anything is whether the count is inclusive. From the 1st to the 3rd is two days if you are measuring elapsed time and three days if you are counting the days involved, and both answers appear in real specifications.
This is not pedantry. Trial periods, SLA windows, retention rules and billing cycles are all built on that choice, and an off-by-one here means a customer is charged a day early or a support ticket breaches a deadline that had not actually passed. When a specification says a 30-day trial, the first thing to clarify is whether day 30 is inside it.
Building test data with relative dates
Adding or subtracting days is how you produce data that exercises time-dependent behaviour without waiting for it: a subscription that expires tomorrow, an invoice that went overdue yesterday, a record just old enough to be archived, a token issued last week. Calculate the date here and seed it, rather than picking a date that looked plausible.
Two habits make this data hold up. Anchor to a fixed reference date in automated tests rather than to today, so a suite that passes in March still passes in November. And make the boundary explicit: if the rule is that records older than 90 days are archived, seed one at 89 days, one at exactly 90 and one at 91.
Why adding a month is ambiguous
Adding days is unambiguous; adding months is not. What is 31 January plus one month? There is no 31 February, so implementations differ: some return 28 February, or 29 in a leap year, and some overflow into 3 March. Both are common, neither is wrong in the abstract, and the two give different answers for a monthly subscription renewal.
The consequence worth testing is what happens on the following cycle. If January 31st renewed on February 28th, does the next renewal fall on March 28th or back on March 31st? A system that drifts will move a customer’s billing date permanently after one short month, which is the kind of defect that surfaces as a billing complaint months later.
Dates worth having in every test set
| Date | Why it is interesting |
|---|---|
| 29 February | Only exists in leap years; anniversary logic has to decide what to do in other years |
| 31 January | Month arithmetic has no natural answer one month later |
| 31 December and 1 January | Year rollover, and week numbering that can put both in the same week |
| The last Sunday in March and October | Daylight saving in much of Europe, where a day is 23 or 25 hours long |
| A Saturday and a Sunday | Business-day rules that must skip weekends |
| A public holiday | Business-day calculations that depend on a calendar the code may not have |
The leap year rule itself is worth stating correctly, because the simplified version is wrong: a year is a leap year if it is divisible by four, except years divisible by 100, unless they are also divisible by 400. That is why 2000 was a leap year and 1900 was not, and why 2100 will not be, which matters for any system calculating far-future dates today.
Calendar days versus business days
A promise of delivery within five days means something different depending on which kind of day is meant, and the difference grows across a weekend or a holiday. Business-day arithmetic also needs a holiday calendar, which is regional, so a system serving several countries needs several calendars, and the absence of one is a defect rather than a missing nicety. This calculator works in calendar days; where a specification says business days, confirm which calendar applies before comparing results.
Frequently asked questions
How do I count the days between two dates?
Subtract the earlier from the later, then decide whether the count is inclusive. Elapsed time between the 1st and the 3rd is two days; counting the days involved gives three. Fix the convention before comparing your number to a specification.
What happens when you add one month to 31 January?
It depends on the implementation. Many libraries clamp to the last day of the target month, giving 28 or 29 February; others overflow into early March. Both behaviours exist in production systems, so a monthly renewal date needs an explicit rule and a test for the following cycle as well.
Which years are leap years?
Years divisible by four, except those divisible by 100, unless they are also divisible by 400. So 2000 was a leap year, 1900 was not, and 2100 will not be. Any date calculation reaching past 2100 should be tested against that exception.
Why is my date off by one day?
Usually a timezone conversion. A date stored as midnight UTC becomes the previous evening in the Americas, so the calendar date shifts. Comparing dates as dates rather than as instants avoids most of it; where a date genuinely means a day in a particular place, store the zone as well.
How should date-dependent tests be written?
Anchor to a fixed reference date rather than to today, and control the clock in the system under test where you can. A suite that depends on the real current date will eventually fail on a leap day, a month boundary or a daylight saving weekend, at a moment when nobody changed any code.
Does this calculator count business days?
No, it works in calendar days. Business-day arithmetic needs a regional holiday calendar, and any system applying it across multiple countries needs one per country. Confirm which calendar a specification assumes before comparing numbers.