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,7 +1,9 @@
"use strict";
exports.differenceInDays = differenceInDays;
var _index = require("./differenceInCalendarDays.js");
var _index2 = require("./toDate.js");
import { normalizeDates } from "./_lib/normalizeDates.js";
import { differenceInCalendarDays } from "./differenceInCalendarDays.js";
/**
* The {@link differenceInDays} function options.
*/
/**
* @name differenceInDays
@@ -19,10 +21,9 @@ var _index2 = require("./toDate.js");
* To ignore DST and only measure exact 24-hour periods, use this instead:
* `Math.trunc(differenceInHours(dateLeft, dateRight)/24)|0`.
*
* @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 days according to the local timezone
*
@@ -57,22 +58,26 @@ var _index2 = require("./toDate.js");
* )
* //=> 92
*/
function differenceInDays(dateLeft, dateRight) {
const _dateLeft = (0, _index2.toDate)(dateLeft);
const _dateRight = (0, _index2.toDate)(dateRight);
const sign = compareLocalAsc(_dateLeft, _dateRight);
const difference = Math.abs(
(0, _index.differenceInCalendarDays)(_dateLeft, _dateRight),
export function differenceInDays(laterDate, earlierDate, options) {
const [laterDate_, earlierDate_] = normalizeDates(
options?.in,
laterDate,
earlierDate,
);
_dateLeft.setDate(_dateLeft.getDate() - sign * difference);
const sign = compareLocalAsc(laterDate_, earlierDate_);
const difference = Math.abs(
differenceInCalendarDays(laterDate_, earlierDate_),
);
laterDate_.setDate(laterDate_.getDate() - sign * difference);
// Math.abs(diff in full days - diff in calendar days) === 1 if last calendar day is not full
// If so, result must be decreased by 1 in absolute value
const isLastDayNotFull = Number(
compareLocalAsc(_dateLeft, _dateRight) === -sign,
compareLocalAsc(laterDate_, earlierDate_) === -sign,
);
const result = sign * (difference - isLastDayNotFull);
// Prevent negative zero
return result === 0 ? 0 : result;
@@ -82,22 +87,22 @@ function differenceInDays(dateLeft, dateRight) {
// for accurate equality comparisons of UTC timestamps that end up
// having the same representation in local time, e.g. one hour before
// DST ends vs. the instant that DST ends.
function compareLocalAsc(dateLeft, dateRight) {
function compareLocalAsc(laterDate, earlierDate) {
const diff =
dateLeft.getFullYear() - dateRight.getFullYear() ||
dateLeft.getMonth() - dateRight.getMonth() ||
dateLeft.getDate() - dateRight.getDate() ||
dateLeft.getHours() - dateRight.getHours() ||
dateLeft.getMinutes() - dateRight.getMinutes() ||
dateLeft.getSeconds() - dateRight.getSeconds() ||
dateLeft.getMilliseconds() - dateRight.getMilliseconds();
laterDate.getFullYear() - earlierDate.getFullYear() ||
laterDate.getMonth() - earlierDate.getMonth() ||
laterDate.getDate() - earlierDate.getDate() ||
laterDate.getHours() - earlierDate.getHours() ||
laterDate.getMinutes() - earlierDate.getMinutes() ||
laterDate.getSeconds() - earlierDate.getSeconds() ||
laterDate.getMilliseconds() - earlierDate.getMilliseconds();
if (diff < 0) {
return -1;
} else if (diff > 0) {
return 1;
// Return 0 if diff is 0; return NaN if diff is NaN
} else {
return diff;
}
if (diff < 0) return -1;
if (diff > 0) return 1;
// Return 0 if diff is 0; return NaN if diff is NaN
return diff;
}
// Fallback for modularized imports:
export default differenceInDays;