日期差计算器
计算两个日期之差,或从某天推算新日期
计算两个日期之差,或从某天推算新日期
两个日期之间的天数差,本质上就是两个日期时间戳的差值除以一天的毫秒数。在 JavaScript 中,一天等于 1000 × 60 × 60 × 24 = 86,400,000 毫秒。
示例:开始日期 2026-05-01,结束日期 2026-05-10,时间戳差值为 9 天 × 86400000 毫秒,相除后得到 9 天。如果开始日期在结束日期之后,结果为负数。
给定一个日期,加上或减去 N 天,实际上就是改变日期的日部分。JavaScript 的 Date 对象提供了 setDate() 方法,会自动处理月份和年份的变化(包括闰年)。
本工具计算的是两个日期间的绝对差值。例如,从 5 月 1 日到 5 月 2 日相差 1 天。如果需要包含首尾两天,请在结果上加 1。
不会。JavaScript 的 Date 对象内建了对闰年和时区的支持,日期相减得到的是 UTC 午夜之间的差值,所以结果完全准确。
是的。例如给 5 月 31 日加 1 天,结果会自动变为 6 月 1 日。JavaScript 的 setDate() 方法会自动处理月份和年份的进位。
Calculate days between two dates, or add/subtract days from a date
The difference in days between two dates is simply the difference in milliseconds divided by the number of milliseconds in a day (86,400,000).
Example: May 1 to May 10, 2026 = 9 days.
JavaScript's setDate() method handles month and year overflow automatically.
No, the result is the exact number of days between the two dates. If you need an inclusive count, add 1 to the result.
Yes, JavaScript's Date engine correctly handles all leap years.
Yes. Adding 1 day to May 31 results in June 1. The setDate() method handles month and year rollovers seamlessly.