Skip to content

A full-featured CRM built with React, shadcn/ui, and Supabase.

handle only date for expected_closing_date and update format to avoid tz issue

ThieryMichela0987345e584
diff --git a/src/components/atomic-crm/deals/DealShow.tsx b/src/components/atomic-crm/deals/DealShow.tsx
index 641d3c98..1c48a8c0 100644
--- a/src/components/atomic-crm/deals/DealShow.tsx
+++ b/src/components/atomic-crm/deals/DealShow.tsx
@@ -47,6 +47,20 @@ export const DealShow = ({ open, id }: { open: boolean; id?: string }) => {
);
};
+const isoDateStringRegex = /^\d{4}-\d{2}-\d{2}$/;
+
+export function formatISODateString(dateString: string) {
+ if (!isoDateStringRegex.test(dateString)) {
+ throw new Error("Invalid date format. Expected YYYY-MM-DD.");
+ }
+ // Some browsers Will consider a date in the format YYYY-MM-DD as UTC, which can cause off-by-one-day issues depending on the user's timezone.
+ // To avoid this, we can parse the date components manually and create a date object in the local timezone.
+ const [year, month, day] = dateString.split("-").map(Number);
+ const date = new Date(year, month - 1, day);
+
+ return format(date, "PP");
+}
+
const DealShowContent = () => {
const { dealStages, dealCategories } = useConfigurationContext();
const record = useRecordContext<Deal>();
@@ -91,7 +105,7 @@ const DealShowContent = () => {
<div className="flex items-center gap-2">
<span className="text-sm">
{isValid(new Date(record.expected_closing_date))
- ? format(new Date(record.expected_closing_date), "PP")
+ ? formatISODateString(record.expected_closing_date)
: "Invalid date"}
</span>
{new Date(record.expected_closing_date) < new Date() ? (
diff --git a/src/lib/utils.ts b/src/lib/utils.ts
index a5ef1935..6fcf374b 100644
--- a/src/lib/utils.ts
+++ b/src/lib/utils.ts
@@ -4,3 +4,4 @@ import { twMerge } from "tailwind-merge";
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
+
diff --git a/supabase/migrations/20260226163952_deals_expected_closing_date_date_only.sql b/supabase/migrations/20260226163952_deals_expected_closing_date_date_only.sql
new file mode 100644
index 00000000..17935a75
--- /dev/null
+++ b/supabase/migrations/20260226163952_deals_expected_closing_date_date_only.sql
@@ -0,0 +1,3 @@
+-- Migrating from timestamp with timezone to date only for expected_closing_date as we don't need the precision
+-- And by only handling the date part in the client it caused conversion issues for users in different timezones
+ALTER table "public"."deals" ALTER COLUMN "expected_closing_date" TYPE date USING "expected_closing_date"::date;