date-vir - v7.4.0
    Preparing search index...

    Function diffDates

    • Diffs two dates and returns the diff in the specified unit or units. When a multiple units are provided with the units input property, an array is returned with the full diff duration contained in each entry in the requested unit.

      Note: when years, quarters, or months are used for the unit, "long term" durations are used to calculate the diff. See more details here: https://moment.github.io/luxon/#/math?id=casual-vs-longterm-conversion-accuracy

      Type Parameters

      • const SelectedUnits extends Readonly<Partial<Record<DurationUnit, undefined | boolean>>>

      Parameters

      Returns DurationBySelection<SelectedUnits>

      import {diffDates, DurationUnit} from 'date-vir';

      const exampleDate: FullDate = {
      year: 2024,
      month: 1,
      day: 5,
      hour: 1,
      minute: 1,
      second: 1,
      millisecond: 1,
      timezone: 'UTC',
      };

      // get the diff in days
      diffDates({start: exampleDate, end: {...exampleDate, day: 6}}, {days: true}); // {days: 1}
      // get the full diff separately in days and also years
      diffDates(
      {
      start: exampleDate,
      end: {
      ...exampleDate,
      day: exampleDate.day + 1,
      hour: exampleDate.hour + 3,
      },
      },
      {
      days: true,
      hours: true,
      },
      );
      // {days: 1, hours: 3}