The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
fix more timezone issues
Steven Teyedff4a31b8fe
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/payouts/payout-table.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/payouts/payout-table.tsxindex 36fc4a1758..f372a1df9f 100644--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/payouts/payout-table.tsx+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/programs/[programId]/payouts/payout-table.tsx@@ -23,7 +23,8 @@ import {useTable,} from "@dub/ui";import { Dots, MoneyBill2, MoneyBills2 } from "@dub/ui/icons";-import { cn, formatDate } from "@dub/utils";+import { cn } from "@dub/utils";+import { formatDate, formatPeriod } from "@dub/utils/src/functions/datetime";import { fetcher } from "@dub/utils/src/functions/fetcher";import { Row } from "@tanstack/react-table";import { Command } from "cmdk";@@ -95,16 +96,7 @@ export function PayoutTable() {{id: "periodStart",header: "Period",- accessorFn: (d) => {- if (!d.periodStart || !d.periodEnd) {- return "-";- }-- return `${formatDate(d.periodStart, { timeZone: "utc", month: "short", year: undefined })}-${formatDate(- d.periodEnd,- { timeZone: "utc", month: "short" },- )}`;- },+ accessorFn: (d) => formatPeriod(d),},{header: "Partner",diff --git a/apps/web/app/partners.dub.co/(dashboard)/settings/payouts/payout-details-sheet.tsx b/apps/web/app/partners.dub.co/(dashboard)/settings/payouts/payout-details-sheet.tsxindex 84ddccae2d..afb4e97ce7 100644--- a/apps/web/app/partners.dub.co/(dashboard)/settings/payouts/payout-details-sheet.tsx+++ b/apps/web/app/partners.dub.co/(dashboard)/settings/payouts/payout-details-sheet.tsx@@ -20,9 +20,11 @@ import {currencyFormatter,DICEBEAR_AVATAR_URL,fetcher,- formatDate,- formatDateTime,} from "@dub/utils";+import {+ formatDateTime,+ formatPeriod,+} from "@dub/utils/src/functions/datetime";import Link from "next/link";import { Dispatch, Fragment, SetStateAction, useMemo } from "react";import useSWR from "swr";@@ -71,17 +73,7 @@ function PayoutDetailsSheetContent({<ExpandingArrow className="size-3" /></a>),- Period:- !payout.periodStart || !payout.periodEnd- ? "-"- : `${formatDate(payout.periodStart, {- month: "short",- year:- new Date(payout.periodStart).getFullYear() ===- new Date(payout.periodEnd).getFullYear()- ? undefined- : "numeric",- })}-${formatDate(payout.periodEnd, { month: "short" })}`,+ Period: formatPeriod(payout),Type: <PayoutTypeBadge type={payout.type} />,diff --git a/apps/web/app/partners.dub.co/(dashboard)/settings/payouts/payout-table.tsx b/apps/web/app/partners.dub.co/(dashboard)/settings/payouts/payout-table.tsxindex b9b9a6537c..533fb479bd 100644--- a/apps/web/app/partners.dub.co/(dashboard)/settings/payouts/payout-table.tsx+++ b/apps/web/app/partners.dub.co/(dashboard)/settings/payouts/payout-table.tsx@@ -14,7 +14,8 @@ import {useTable,} from "@dub/ui";import { MoneyBill2 } from "@dub/ui/icons";-import { DICEBEAR_AVATAR_URL, formatDate } from "@dub/utils";+import { DICEBEAR_AVATAR_URL } from "@dub/utils";+import { formatPeriod } from "@dub/utils/src/functions/datetime";import { fetcher } from "@dub/utils/src/functions/fetcher";import { useEffect, useState } from "react";import useSWR from "swr";@@ -68,16 +69,7 @@ export function PayoutTable() {{id: "periodStart",header: "Period",- accessorFn: (d) => {- if (!d.periodStart || !d.periodEnd) {- return "-";- }-- return `${formatDate(d.periodStart, { month: "short", year: new Date(d.periodStart).getFullYear() === new Date(d.periodEnd).getFullYear() ? undefined : "numeric" })}-${formatDate(- d.periodEnd,- { month: "short" },- )}`;- },+ accessorFn: (d) => formatPeriod(d),},{header: "Program",diff --git a/apps/web/ui/partners/partner-details-sheet.tsx b/apps/web/ui/partners/partner-details-sheet.tsxindex be27b811a0..d3346460c9 100644--- a/apps/web/ui/partners/partner-details-sheet.tsx+++ b/apps/web/ui/partners/partner-details-sheet.tsx@@ -24,10 +24,10 @@ import {COUNTRIES,currencyFormatter,DICEBEAR_AVATAR_URL,- formatDate,getPrettyUrl,nFormatter,} from "@dub/utils";+import { formatPeriod } from "@dub/utils/src/functions/datetime";import { ChevronLeft } from "lucide-react";import { useAction } from "next-safe-action/hooks";import Link from "next/link";@@ -473,10 +473,8 @@ function PartnerPayouts({ partner }: { partner: EnrolledPartnerProps }) {error: payoutsError ? "Failed to load payouts" : undefined,columns: [{- id: "periodEnd",- header: "Period End",- accessorFn: (d) =>- d.periodStart ? formatDate(d.periodStart, { month: "short" }) : "-",+ header: "Period",+ accessorFn: (d) => formatPeriod(d),},{header: "Status",diff --git a/apps/web/ui/partners/payout-details-sheet.tsx b/apps/web/ui/partners/payout-details-sheet.tsxindex c4d1a0e19c..db884f46e5 100644--- a/apps/web/ui/partners/payout-details-sheet.tsx+++ b/apps/web/ui/partners/payout-details-sheet.tsx@@ -17,9 +17,11 @@ import {currencyFormatter,DICEBEAR_AVATAR_URL,fetcher,- formatDate,- formatDateTime,} from "@dub/utils";+import {+ formatDateTime,+ formatPeriod,+} from "@dub/utils/src/functions/datetime";import Link from "next/link";import { useParams } from "next/navigation";import { Dispatch, Fragment, SetStateAction, useMemo, useState } from "react";@@ -73,17 +75,7 @@ function PayoutDetailsSheetContent({</a>),- Period:- !payout.periodStart || !payout.periodEnd- ? "-"- : `${formatDate(payout.periodStart, {- month: "short",- year:- new Date(payout.periodStart).getFullYear() ===- new Date(payout.periodEnd).getFullYear()- ? undefined- : "numeric",- })}-${formatDate(payout.periodEnd, { month: "short" })}`,+ Period: formatPeriod(payout),Type: <PayoutTypeBadge type={payout.type} />,diff --git a/packages/utils/src/functions/datetime.ts b/packages/utils/src/functions/datetime.tsdeleted file mode 100644index 25e594b45f..0000000000--- a/packages/utils/src/functions/datetime.ts+++ /dev/null@@ -1,108 +0,0 @@-import * as chrono from "chrono-node";--export const getDateTimeLocal = (timestamp?: Date): string => {- const d = timestamp ? new Date(timestamp) : new Date();- if (d.toString() === "Invalid Date") return "";- return new Date(d.getTime() - d.getTimezoneOffset() * 60000)- .toISOString()- .split(":")- .slice(0, 2)- .join(":");-};--// Function to parse a date string into a Date object-export const parseDateTime = (str: Date | string) => {- if (str instanceof Date) return str;- return chrono.parseDate(str);-};--export const formatDate = (- datetime: Date | string,- options?: Intl.DateTimeFormatOptions,-) => {- if (datetime.toString() === "Invalid Date") return "";- return new Date(datetime).toLocaleDateString("en-US", {- day: "numeric",- month: "long",- year: "numeric",- ...options,- });-};--export const formatDateTime = (- datetime: Date | string,- options?: Intl.DateTimeFormatOptions,-) => {- if (datetime.toString() === "Invalid Date") return "";- return new Date(datetime).toLocaleTimeString("en-US", {- month: "short",- day: "numeric",- year: "numeric",- hour: "numeric",- minute: "numeric",- hour12: true,- ...options,- });-};--// 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));-};--export const getFirstAndLastDay = (day: number) => {- const today = new Date();- const currentDay = today.getDate();- const currentMonth = today.getMonth();- const currentYear = today.getFullYear();- if (currentDay >= day) {- // if the current day is greater than target day, it means that we just passed it- return {- firstDay: new Date(currentYear, currentMonth, day),- lastDay: new Date(currentYear, currentMonth + 1, day - 1),- };- } else {- // if the current day is less than target day, it means that we haven't passed it yet- const lastYear = currentMonth === 0 ? currentYear - 1 : currentYear; // if the current month is January, we need to go back a year- const lastMonth = currentMonth === 0 ? 11 : currentMonth - 1; // if the current month is January, we need to go back to December- return {- firstDay: new Date(lastYear, lastMonth, day),- lastDay: new Date(currentYear, currentMonth, day - 1),- };- }-};--// Function to get the last day of the current month-export const getLastDayOfMonth = () => {- const today = new Date();- const lastDay = new Date(today.getFullYear(), today.getMonth() + 1, 0); // This will give the last day of the current month- return lastDay.getDate();-};--// Adjust the billingCycleStart based on the number of days in the current month-export const getAdjustedBillingCycleStart = (billingCycleStart: number) => {- const lastDay = getLastDayOfMonth();- if (billingCycleStart > lastDay) {- return lastDay;- } else {- return billingCycleStart;- }-};--export const getBillingStartDate = (billingCycleStart: number) => {- const today = new Date();- const currentDay = today.getDate();- const currentMonth = today.getMonth();- const currentYear = today.getFullYear();- const adjustedBillingCycleStart =- getAdjustedBillingCycleStart(billingCycleStart);- if (currentDay <= adjustedBillingCycleStart) {- // if the current day is less than the billing cycle start, we need to go back a month- const lastMonth = currentMonth === 0 ? 11 : currentMonth - 1; // if the current month is January, we need to go back to December- const lastYear = currentMonth === 0 ? currentYear - 1 : currentYear; // if the current month is January, we need to go back a year- return new Date(lastYear, lastMonth, adjustedBillingCycleStart);- } else {- return new Date(currentYear, currentMonth, adjustedBillingCycleStart);- }-};diff --git a/packages/utils/src/functions/datetime/billing-utils.ts b/packages/utils/src/functions/datetime/billing-utils.tsnew file mode 100644index 0000000000..c0f0ec4a03--- /dev/null+++ b/packages/utils/src/functions/datetime/billing-utils.ts@@ -0,0 +1,33 @@+// Function to get the last day of the current month+export const getLastDayOfMonth = () => {+ const today = new Date();+ const lastDay = new Date(today.getFullYear(), today.getMonth() + 1, 0); // This will give the last day of the current month+ return lastDay.getDate();+};++// Adjust the billingCycleStart based on the number of days in the current month+export const getAdjustedBillingCycleStart = (billingCycleStart: number) => {+ const lastDay = getLastDayOfMonth();+ if (billingCycleStart > lastDay) {+ return lastDay;+ } else {+ return billingCycleStart;+ }+};++export const getBillingStartDate = (billingCycleStart: number) => {+ const today = new Date();+ const currentDay = today.getDate();+ const currentMonth = today.getMonth();+ const currentYear = today.getFullYear();+ const adjustedBillingCycleStart =+ getAdjustedBillingCycleStart(billingCycleStart);+ if (currentDay <= adjustedBillingCycleStart) {+ // if the current day is less than the billing cycle start, we need to go back a month+ const lastMonth = currentMonth === 0 ? 11 : currentMonth - 1; // if the current month is January, we need to go back to December+ const lastYear = currentMonth === 0 ? currentYear - 1 : currentYear; // if the current month is January, we need to go back a year+ return new Date(lastYear, lastMonth, adjustedBillingCycleStart);+ } else {+ return new Date(currentYear, currentMonth, adjustedBillingCycleStart);+ }+};diff --git a/packages/utils/src/functions/datetime/format-date.ts b/packages/utils/src/functions/datetime/format-date.tsnew file mode 100644index 0000000000..cb5215a099--- /dev/null+++ b/packages/utils/src/functions/datetime/format-date.ts@@ -0,0 +1,12 @@+export const formatDate = (+ datetime: Date | string,+ options?: Intl.DateTimeFormatOptions,+) => {+ if (datetime.toString() === "Invalid Date") return "";+ return new Date(datetime).toLocaleDateString("en-US", {+ day: "numeric",+ month: "long",+ year: "numeric",+ ...options,+ });+};diff --git a/packages/utils/src/functions/datetime/format-datetime.ts b/packages/utils/src/functions/datetime/format-datetime.tsnew file mode 100644index 0000000000..cc34ce914d--- /dev/null+++ b/packages/utils/src/functions/datetime/format-datetime.ts@@ -0,0 +1,15 @@+export const formatDateTime = (+ datetime: Date | string,+ options?: Intl.DateTimeFormatOptions,+) => {+ if (datetime.toString() === "Invalid Date") return "";+ return new Date(datetime).toLocaleTimeString("en-US", {+ month: "short",+ day: "numeric",+ year: "numeric",+ hour: "numeric",+ minute: "numeric",+ hour12: true,+ ...options,+ });+};diff --git a/packages/utils/src/functions/datetime/format-period.ts b/packages/utils/src/functions/datetime/format-period.tsnew file mode 100644index 0000000000..a68d8f3083--- /dev/null+++ b/packages/utils/src/functions/datetime/format-period.ts@@ -0,0 +1,23 @@+import { formatDate } from "../..";++export const formatPeriod = (d: {+ periodStart?: Date | null;+ periodEnd?: Date | null;+}) => {+ if (!d.periodStart || !d.periodEnd) {+ return "-";+ }++ return `${formatDate(d.periodStart, {+ month: "short",+ year:+ new Date(d.periodStart).getUTCFullYear() ===+ new Date(d.periodEnd).getUTCFullYear()+ ? undefined+ : "numeric",+ timeZone: "utc",+ })}-${formatDate(d.periodEnd, {+ month: "short",+ timeZone: "utc",+ })}`;+};diff --git a/packages/utils/src/functions/datetime/get-datetime-local.ts b/packages/utils/src/functions/datetime/get-datetime-local.tsnew file mode 100644index 0000000000..6aa4182bd0--- /dev/null+++ b/packages/utils/src/functions/datetime/get-datetime-local.ts@@ -0,0 +1,9 @@+export const getDateTimeLocal = (timestamp?: Date): string => {+ const d = timestamp ? new Date(timestamp) : new Date();+ if (d.toString() === "Invalid Date") return "";+ return new Date(d.getTime() - d.getTimezoneOffset() * 60000)+ .toISOString()+ .split(":")+ .slice(0, 2)+ .join(":");+};diff --git a/packages/utils/src/functions/datetime/get-days-difference.ts b/packages/utils/src/functions/datetime/get-days-difference.tsnew file mode 100644index 0000000000..e27d0b190a--- /dev/null+++ b/packages/utils/src/functions/datetime/get-days-difference.ts@@ -0,0 +1,5 @@+// 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/get-first-and-last-day.ts b/packages/utils/src/functions/datetime/get-first-and-last-day.tsnew file mode 100644index 0000000000..7e9e1aa9ae--- /dev/null+++ b/packages/utils/src/functions/datetime/get-first-and-last-day.ts@@ -0,0 +1,21 @@+export const getFirstAndLastDay = (day: number) => {+ const today = new Date();+ const currentDay = today.getDate();+ const currentMonth = today.getMonth();+ const currentYear = today.getFullYear();+ if (currentDay >= day) {+ // if the current day is greater than target day, it means that we just passed it+ return {+ firstDay: new Date(currentYear, currentMonth, day),+ lastDay: new Date(currentYear, currentMonth + 1, day - 1),+ };+ } else {+ // if the current day is less than target day, it means that we haven't passed it yet+ const lastYear = currentMonth === 0 ? currentYear - 1 : currentYear; // if the current month is January, we need to go back a year+ const lastMonth = currentMonth === 0 ? 11 : currentMonth - 1; // if the current month is January, we need to go back to December+ return {+ firstDay: new Date(lastYear, lastMonth, day),+ lastDay: new Date(currentYear, currentMonth, day - 1),+ };+ }+};diff --git a/packages/utils/src/functions/datetime/index.ts b/packages/utils/src/functions/datetime/index.tsnew file mode 100644index 0000000000..52b3aaab86--- /dev/null+++ b/packages/utils/src/functions/datetime/index.ts@@ -0,0 +1,8 @@+export * from "./billing-utils";+export * from "./format-date";+export * from "./format-datetime";+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";diff --git a/packages/utils/src/functions/datetime/parse-datetime.ts b/packages/utils/src/functions/datetime/parse-datetime.tsnew file mode 100644index 0000000000..32fd29bfda--- /dev/null+++ b/packages/utils/src/functions/datetime/parse-datetime.ts@@ -0,0 +1,7 @@+import * as chrono from "chrono-node";++// Function to parse a date string into a Date object+export const parseDateTime = (str: Date | string) => {+ if (str instanceof Date) return str;+ return chrono.parseDate(str);+};