How to load SQL pure Date type. #29168
Replies: 2 comments 2 replies
|
Hey! In the schema are you currently mapping SQL DATE to You can set TZ=UTC as an environment variable before your Node process starts. This makes local and UTC methods return the same thing. Simple but can have side effects if you actually need local time elsewhere. |
|
You're not using Prisma wrong, and the data in the DB is fine — this is a pure JavaScript timezone gotcha, not a Prisma bug. What's actually happening A SQL That value is correct. The off-by-one only appears when you read it with the local getters, because those re-interpret that instant in the process timezone: const d = new Date("2000-01-01T00:00:00.000Z"); // what Prisma returns
// process running in America/Los_Angeles:
d.getFullYear(); // 1999 ❌ (local: still Dec 31 1999 there)
d.getUTCFullYear(); // 2000 ✅
d.toISOString().slice(0, 10); // "2000-01-01" ✅So nothing is lost — you just have to read the date in UTC, since UTC is the timezone Prisma encoded the date-only value in. Fix 1 — read with UTC getters (or format in UTC) Anywhere you pull components off the date, use the d.getUTCFullYear(); // 2000
d.getUTCMonth(); // 0
d.getUTCDate(); // 1
new Intl.DateTimeFormat('en-CA', { timeZone: 'UTC' }).format(d); // "2000-01-01"Fix 2 — add a "custom loader" via a Prisma Client result extension There's no per-field decoder hook in the engine, but result extensions are exactly the supported way to transform a value on read. Add a computed field that always returns the correct date-only value, derived from the UTC parts so it's timezone-proof: const prisma = new PrismaClient().$extends({
result: {
event: { // your model name
eventDate: { // your @db.Date field — pick any name for the computed one
needs: { date: true },
compute(row) {
// row.date is the UTC-midnight Date; format it in UTC
return row.date.toISOString().slice(0, 10); // "2000-01-01"
// or return a "floating" local-midnight Date if you prefer a Date:
// const d = row.date;
// return new Date(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate());
},
},
},
},
});Computed fields are type-safe and computed on access (docs). The constraint is they must be based on scalar fields (which Fix 3 — global TZ=UTC (the blunt instrument) Setting TL;DR: the stored date is right; treat |
Uh oh!
There was an error while loading. Please reload this page.
Question
We are using Prisma to read from an already existing SQL database.
This Database has a lot of Date fields (not DateTime)
Prisma seems to be loading those values by calling
new Date()The issue with this is that javascript assumes the pure Date is midnight UTC when calling new, but uses server timezone for other operations.
So
Date.new("2000-01-01").getFullYear()returns 1999 if your sever is anywhere west of London.Which means that all our dates are wrong after being loaded by Prisma.
Am using it wrong somehow? Anyway to add custom loaders?
How to reproduce (optional)
Expected behavior (optional)
No response
Information about Prisma Schema, Client Queries and Environment (optional)
// Add your schema.prisma// Add any relevant Prisma Client queries hereOS:
Database:
Node.js version:
Run
prisma -vto see your Prisma version and paste itAll reactions