chronia
    Preparing search index...

    Function diffMonths

    • Calculate the difference in calendar months between two dates.

      This function calculates the number of full calendar months between two dates. Only year and month values are considered; days and time components are ignored.

      Parameters

      • dateLeft: number | Date

        The first date as a Date object or timestamp (number)

      • dateRight: number | Date

        The second date as a Date object or timestamp (number)

      Returns number

      The difference in calendar months (negative if dateLeft is before dateRight), or NaN if any input is invalid

      // Basic month difference
      const result = diffMonths(new Date(2024, 5, 15), new Date(2024, 2, 1));
      // Returns: 3

      // Days are ignored (same month)
      const result = diffMonths(new Date(2024, 5, 30), new Date(2024, 5, 1));
      // Returns: 0

      // Works with timestamps
      const timestamp1 = new Date(2025, 2, 1).getTime();
      const timestamp2 = new Date(2024, 2, 31).getTime();
      const result = diffMonths(timestamp1, timestamp2);
      // Returns: 12

      // Negative result when first date is earlier
      const result = diffMonths(new Date(2024, 2, 15), new Date(2024, 5, 15));
      // Returns: -3

      // Invalid inputs return NaN
      const result = diffMonths(new Date("invalid"), new Date(2024, 5, 15));
      // Returns: NaN
      • Considers only year and month values for calculation
      • Days and time components (hours, minutes, seconds, milliseconds) are ignored
      • Accepts both Date objects and numeric timestamps
      • Returns NaN for: Invalid Date, NaN, Infinity, -Infinity
      • Handles year boundaries correctly
      • Calculation: (yearDiff * 12) + monthDiff