registration

This commit is contained in:
User
2025-02-02 16:08:03 +03:00
parent 7f6495eb4d
commit 78afbaed71
6334 changed files with 196774 additions and 165754 deletions

View File

@@ -1,8 +1,10 @@
"use strict";
exports.differenceInYears = differenceInYears;
var _index = require("./compareAsc.js");
var _index2 = require("./differenceInCalendarYears.js");
var _index3 = require("./toDate.js");
import { normalizeDates } from "./_lib/normalizeDates.js";
import { compareAsc } from "./compareAsc.js";
import { differenceInCalendarYears } from "./differenceInCalendarYears.js";
/**
* The {@link differenceInYears} function options.
*/
/**
* @name differenceInYears
@@ -12,10 +14,9 @@ var _index3 = require("./toDate.js");
* @description
* Get the number of full years between the given dates.
*
* @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
*
* @param dateLeft - The later date
* @param dateRight - The earlier date
* @param laterDate - The later date
* @param earlierDate - The earlier date
* @param options - An object with options
*
* @returns The number of full years
*
@@ -24,26 +25,39 @@ var _index3 = require("./toDate.js");
* const result = differenceInYears(new Date(2015, 1, 11), new Date(2013, 11, 31))
* //=> 1
*/
function differenceInYears(dateLeft, dateRight) {
const _dateLeft = (0, _index3.toDate)(dateLeft);
const _dateRight = (0, _index3.toDate)(dateRight);
const sign = (0, _index.compareAsc)(_dateLeft, _dateRight);
const difference = Math.abs(
(0, _index2.differenceInCalendarYears)(_dateLeft, _dateRight),
export function differenceInYears(laterDate, earlierDate, options) {
const [laterDate_, earlierDate_] = normalizeDates(
options?.in,
laterDate,
earlierDate,
);
// Set both dates to a valid leap year for accurate comparison when dealing
// with leap days
_dateLeft.setFullYear(1584);
_dateRight.setFullYear(1584);
// -1 if the left date is earlier than the right date
// 2023-12-31 - 2024-01-01 = -1
const sign = compareAsc(laterDate_, earlierDate_);
// Math.abs(diff in full years - diff in calendar years) === 1 if last calendar year is not full
// If so, result must be decreased by 1 in absolute value
const isLastYearNotFull =
(0, _index.compareAsc)(_dateLeft, _dateRight) === -sign;
const result = sign * (difference - +isLastYearNotFull);
// 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;