Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 2x 2x 12x 12x 12x 96x 96x 12x | import { Duration, DateInput } from './types';
import { ZERO, UNITS_META } from './lib/units';
/**
* Gets the difference between two dates, expressed as a duration object.
*
* @example
* between('2022-01-01', '2020-01-01') // { years: -2, months: 0, weeks: 0, ... }
*/
export const between = (date1: DateInput, date2: DateInput): Duration => {
const a = new Date(date1);
const b = new Date(date2);
const output: Duration = { ...ZERO };
UNITS_META.forEach(({ unit, dateGetter }) => {
output[unit] = dateGetter(b) - dateGetter(a);
});
return output;
};
|