教程 date-fns tutorial

intervalToDuration

返回两个日期之间的持续时间对象。

javascript
import { intervalToDuration } from "date-fns";

// 示例日期
const startDate = new Date(2024, 0, 1); // 2024年1月1日
const endDate = new Date(2024, 2, 15); // 2024年3月15日

// 计算持续时间
const duration = intervalToDuration({ start: startDate, end: endDate });

console.log("持续时间对象:", duration); // 持续时间对象: { years: 0, months: 2, days: 14, hours: 0, minutes: 0, seconds: 0 }