Chris
Chris
DTDrizzle Team
Created by Abdulramon Jemil on 9/8/2024 in #help
How does Drizzle ORM map PostgreSQL dates with regard to timezones?
I use a custom type localDate to work around that.
import { formatISO, parse } from 'date-fns';
import { customType } from 'drizzle-orm/pg-core';

export const localDate = customType<{
data: Date;
driverData: string;
config: {};
}>({
dataType() {
return 'date';
},
fromDriver(value: string): Date {
return parse(value, 'yyyy-MM-dd', new Date());
},
toDriver(value: Date): string {
return formatISO(value, { representation: 'date' });
},
});
import { formatISO, parse } from 'date-fns';
import { customType } from 'drizzle-orm/pg-core';

export const localDate = customType<{
data: Date;
driverData: string;
config: {};
}>({
dataType() {
return 'date';
},
fromDriver(value: string): Date {
return parse(value, 'yyyy-MM-dd', new Date());
},
toDriver(value: Date): string {
return formatISO(value, { representation: 'date' });
},
});
10 replies
DTDrizzle Team
Created by Abdulramon Jemil on 9/8/2024 in #help
How does Drizzle ORM map PostgreSQL dates with regard to timezones?
How do I override the mapping of date in the Pg driver?
10 replies
DTDrizzle Team
Created by Abdulramon Jemil on 9/8/2024 in #help
How does Drizzle ORM map PostgreSQL dates with regard to timezones?
When I use date-fns's format function to format a date return from drizzle, it's always one day less than the stored date in the database.
10 replies
DTDrizzle Team
Created by Abdulramon Jemil on 9/8/2024 in #help
How does Drizzle ORM map PostgreSQL dates with regard to timezones?
I need advice on how to work around a problem with this mapping. When mapping date from postgres back to JS Date, new Date(aPostgresDateString) will produces a Date object with timezone. new Date assumes the date string to be UTC. i.e., 2024-11-24 is equivalent to "2024-11-24 00:00:000Z" So new Date produces time object really. 2024-11-24 becomes Sun Nov 23 2024 19:00:00 GMT-0500. The problem arises when date manipulation library and UI components truncate the time part of the Date object and only works with the date part.
10 replies