@lonu/stc
Version:
A tool for converting OpenApi/Swagger/Apifox into code.
117 lines (116 loc) • 3.71 kB
JavaScript
// Copyright 2018-2025 the Deno authors. MIT license.
// This module is browser compatible.
import { DAY, HOUR, MINUTE, SECOND, WEEK } from "./constants.js";
function calculateMonthsDifference(from, to) {
let months = (to.getFullYear() - from.getFullYear()) * 12 +
(to.getMonth() - from.getMonth());
if (to.getDate() < from.getDate()) {
months--;
}
return months;
}
/**
* Calculates the difference of the 2 given dates in various units. If the units
* are omitted, it returns the difference in the all available units.
*
* @param from Year to calculate difference from.
* @param to Year to calculate difference to.
* @param options Options such as units to calculate difference in.
* @returns The difference of the 2 given dates in various units.
*
* @example Basic usage
* ```ts
* import { difference } from "@std/datetime/difference";
* import { assertEquals } from "@std/assert";
*
* const date0 = new Date("2018-05-14");
* const date1 = new Date("2020-05-13");
*
* assertEquals(difference(date0, date1), {
* milliseconds: 63072000000,
* seconds: 63072000,
* minutes: 1051200,
* hours: 17520,
* days: 730,
* weeks: 104,
* months: 23,
* quarters: 7,
* years: 1
* });
* ```
*
* @example Calculate difference in specific units
*
* The `units` option defines which units to calculate the difference in.
*
* ```ts
* import { difference } from "@std/datetime/difference";
* import { assertEquals } from "@std/assert";
*
* const date0 = new Date("2018-05-14");
* const date1 = new Date("2020-05-13");
*
* const result = difference(date0, date1, { units: ["days", "months", "years"] });
*
* assertEquals(result, {
* days: 730,
* months: 23,
* years: 1
* });
* ```
*/
export function difference(from, to, options) {
[from, to] = from < to ? [from, to] : [to, from];
const uniqueUnits = options?.units ? [...new Set(options?.units)] : [
"milliseconds",
"seconds",
"minutes",
"hours",
"days",
"weeks",
"months",
"quarters",
"years",
];
const differenceInMs = Math.abs(from.getTime() - to.getTime());
const differences = {};
let months = null;
for (const uniqueUnit of uniqueUnits) {
switch (uniqueUnit) {
case "milliseconds":
differences.milliseconds = differenceInMs;
break;
case "seconds":
differences.seconds = Math.floor(differenceInMs / SECOND);
break;
case "minutes":
differences.minutes = Math.floor(differenceInMs / MINUTE);
break;
case "hours":
differences.hours = Math.floor(differenceInMs / HOUR);
break;
case "days":
differences.days = Math.floor(differenceInMs / DAY);
break;
case "weeks":
differences.weeks = Math.floor(differenceInMs / WEEK);
break;
case "months":
if (months === null)
months = calculateMonthsDifference(from, to);
differences.months = months;
break;
case "quarters":
if (months === null)
months = calculateMonthsDifference(from, to);
differences.quarters = Math.floor(months / 3);
break;
case "years":
if (months === null)
months = calculateMonthsDifference(from, to);
differences.years = Math.floor(months / 12);
break;
}
}
return differences;
}