運作原理
There are two different questions hidden in the phrase difference between two dates. The exact elapsed duration is a fixed number of milliseconds, which divides cleanly into days, hours and minutes. The calendar difference is what a person means by three years and two months, and it depends on where the dates fall: from 31 January to 28 February is one calendar month, but so is 31 January to 3 March. This tool reports both, because a contract that runs for one calendar month and one that runs for 28 days are genuinely different agreements.
The calendar difference is computed with borrowing, the way subtraction is taught at school. When the day component of the end date is smaller than that of the start date, a month is borrowed from the month component and the number of days in the previous month is added — which is why the algorithm has to know that February 2024 had 29 days and February 2025 had 28. Getting this wrong is what produces the off-by-one-day errors that appear in billing systems and in age calculations at exactly the boundaries where they matter. A 29 February birthday, for instance, is a well-known edge case: some jurisdictions treat the person as ageing on 28 February in non-leap years and others on 1 March.
Business-day counting is arithmetic on a calendar rather than on a number line. Weekends are removed first, using a configurable mask because the working week is not Saturday and Sunday everywhere — much of the Middle East observes Friday and Saturday. Public holidays are then removed from the remaining set, taking into account that a holiday falling on a weekend does not remove an additional working day, and that substitute holidays exist in some countries to compensate. The result is the count a project manager or an SLA calculation actually needs.
Duration arithmetic in the other direction has its own traps. Adding one month to 31 January gives 28 or 29 February, not 31 February, so the day is clamped to the last valid day of the target month. Adding one year to 29 February in a leap year gives 28 February. Adding a number of days is straightforward in contrast, because a day is a fixed amount of time — except across a daylight saving transition, where the local time shifts by an hour but the date does not, which is why the arithmetic here works on calendar dates rather than on elapsed milliseconds where the question is a calendar one.
常見應用情境
- Count the days remaining until a deadline, a renewal or a product launch.
- Work out an exact age in years, months and days for a form or a record.
- Count the working days in a project window, excluding weekends and public holidays.
- Calculate a due date ninety days out for an invoice or a notice period.
- Check whether a subscription period has elapsed in calendar months or in days.
- Work out how long an employee has been with a company in years and months for a service award.
最後更新: 2026-10-14 — 正文以英文顯示,介面已完成翻譯。