Rocky_Mountain_Vending/.pnpm-store/v10/files/52/bf090aef72414e7bb2928a099777b94e7eb7bbb490b6e957c0c7a8cb2ad18c2320f16917ab5c44c3c3b686e6c5ee08d724cdc7c5f5a6c224c1e12a36a7675f
DMleadgen 46d973904b
Initial commit: Rocky Mountain Vending website
Next.js website for Rocky Mountain Vending company featuring:
- Product catalog with Stripe integration
- Service areas and parts pages
- Admin dashboard with Clerk authentication
- SEO optimized pages with JSON-LD structured data

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-12 16:22:15 -07:00

63 lines
2.1 KiB
Text

import { normalizeDates } from "./_lib/normalizeDates.js";
import { compareAsc } from "./compareAsc.js";
import { differenceInCalendarYears } from "./differenceInCalendarYears.js";
/**
* The {@link differenceInYears} function options.
*/
/**
* @name differenceInYears
* @category Year Helpers
* @summary Get the number of full years between the given dates.
*
* @description
* Get the number of full years between the given dates.
*
* @param laterDate - The later date
* @param earlierDate - The earlier date
* @param options - An object with options
*
* @returns The number of full years
*
* @example
* // How many full years are between 31 December 2013 and 11 February 2015?
* const result = differenceInYears(new Date(2015, 1, 11), new Date(2013, 11, 31))
* //=> 1
*/
export function differenceInYears(laterDate, earlierDate, options) {
const [laterDate_, earlierDate_] = normalizeDates(
options?.in,
laterDate,
earlierDate,
);
// -1 if the left date is earlier than the right date
// 2023-12-31 - 2024-01-01 = -1
const sign = compareAsc(laterDate_, earlierDate_);
// First calculate the difference in calendar years
// 2024-01-01 - 2023-12-31 = 1 year
const diff = Math.abs(differenceInCalendarYears(laterDate_, earlierDate_));
// Now we need to calculate if the difference is full. To do that we set
// both dates to the same year and check if the both date's month and day
// form a full year.
laterDate_.setFullYear(1584);
earlierDate_.setFullYear(1584);
// For it to be true, when the later date is indeed later than the earlier date
// (2026-02-01 - 2023-12-10 = 3 years), the difference is full if
// the normalized later date is also later than the normalized earlier date.
// In our example, 1584-02-01 is earlier than 1584-12-10, so the difference
// is partial, hence we need to subtract 1 from the difference 3 - 1 = 2.
const partial = compareAsc(laterDate_, earlierDate_) === -sign;
const result = sign * (diff - +partial);
// Prevent negative zero
return result === 0 ? 0 : result;
}
// Fallback for modularized imports:
export default differenceInYears;