Skip to content

Function: diff()

ts
function diff(
   dateLeft, 
   dateRight, 
   unit): number;

Signed difference from dateLeft to dateRight in the given unit. Years/months use calendar arithmetic; days/weeks align to start-of-day/week. 从 dateLeftdateRight 的带符号差值;年/月按日历对齐,日/周对齐到自然日/周起始。

Parameters

ParameterType
dateLeftDate
dateRightDate
unitDiffUnit

Returns

number

Example

ts
diff(new Date('2026-05-01'), new Date('2026-05-08'), 'days') // => 7
diff(new Date('2026-01-01'), new Date('2026-04-01'), 'months') // => 3

Since

1.0.0

Released under the MIT License.