Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

refactor `getDaysDifference` to use `differenceInDays`

Steven Tey3002ed5ce3d1
diff --git a/apps/web/lib/analytics/format-date-tooltip.ts b/apps/web/lib/analytics/format-date-tooltip.ts
index 8873122df9..b41a0e7c45 100644
--- a/apps/web/lib/analytics/format-date-tooltip.ts
+++ b/apps/web/lib/analytics/format-date-tooltip.ts
@@ -1,4 +1,4 @@
-import { getDaysDifference } from "@dub/utils";
+import { differenceInDays } from "date-fns";
export const formatDateTooltip = (
date: Date,
@@ -27,7 +27,7 @@ export const formatDateTooltip = (
}
if (start && end) {
- const daysDifference = getDaysDifference(
+ const daysDifference = differenceInDays(
typeof start === "string" ? new Date(start) : start,
typeof end === "string" ? new Date(end) : end,
);
diff --git a/apps/web/lib/analytics/utils/valid-date-range-for-plan.ts b/apps/web/lib/analytics/utils/valid-date-range-for-plan.ts
index 7cd0875f0b..881089ab61 100644
--- a/apps/web/lib/analytics/utils/valid-date-range-for-plan.ts
+++ b/apps/web/lib/analytics/utils/valid-date-range-for-plan.ts
@@ -1,4 +1,4 @@
-import { getDaysDifference } from "@dub/utils";
+import { differenceInDays } from "date-fns";
export type DateRangeValidationResult =
| { valid: true }
@@ -32,7 +32,7 @@ export const validDateRangeForPlan = ({
(interval === "90d" ||
interval === "1y" ||
interval === "ytd" ||
- (start && getDaysDifference(new Date(start), end || now) > 31))
+ (start && differenceInDays(start, end || now) > 31))
) {
return {
valid: false,
@@ -43,11 +43,7 @@ export const validDateRangeForPlan = ({
}
// Pro plan users can only get analytics for 1 year
- if (
- plan === "pro" &&
- start &&
- getDaysDifference(new Date(start), end || now) > 366
- ) {
+ if (plan === "pro" && start && differenceInDays(start, end || now) > 366) {
return {
valid: false,
code: "pro-limit",
diff --git a/packages/utils/src/functions/datetime/get-days-difference.ts b/packages/utils/src/functions/datetime/get-days-difference.ts
deleted file mode 100644
index e27d0b190a..0000000000
--- a/packages/utils/src/functions/datetime/get-days-difference.ts
+++ /dev/null
@@ -1,5 +0,0 @@
-// Function to get the number of days between two dates
-export const getDaysDifference = (startDate: Date, endDate: Date) => {
- const diffTime = Math.abs(endDate.getTime() - startDate.getTime());
- return Math.ceil(diffTime / (1000 * 60 * 60 * 24));
-};
diff --git a/packages/utils/src/functions/datetime/index.ts b/packages/utils/src/functions/datetime/index.ts
index 86342d6a13..bd6244cdda 100644
--- a/packages/utils/src/functions/datetime/index.ts
+++ b/packages/utils/src/functions/datetime/index.ts
@@ -5,6 +5,5 @@ export * from "./format-datetime";
export * from "./format-datetime-smart";
export * from "./format-period";
export * from "./get-datetime-local";
-export * from "./get-days-difference";
export * from "./get-first-and-last-day";
export * from "./parse-datetime";