question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow passing `YYYY-MM-DD` in native database date types to avoid timezone issues

See original GitHub issue

Bug description

When I insert a date value, it gets stored in the database date attribute with a day off by one.

How to reproduce

I create a date object via new Date(1960, 8, 24) and I insert it on both a date and a datetime attribute of a table record.

  • In the date attribute xdate I find “1960-09-23” which to me is unexpected behaviour.

  • In the datetime attribute xdatetime I find “1960-09-24” which I would expect.

  • schema.prisma was generated by npx prisma db pull

import pkg from "@prisma/client";

const { PrismaClient } = pkg;
const prisma = new PrismaClient();

(async () => {
  // create a date 24.9.1960
  const now = new Date(1960, 8, 24);

  console.log(now);

  console.log(`Original date: ${format(now, "dd.MM.yyyy")}`);
  const name = "Demo";

  const nowFromDB = await prisma.myTable.create({
    data: { name: name, xdate: now, xdatetime: now },
  });
  console.log("Database date");
  console.log(nowFromDB);
  console.log(`Database date: ${format(nowFromDB.xdate, "dd.MM.yyyy")}`);
  console.log(`Database date: ${format(nowFromDB.xdatetime, "dd.MM.yyyy")}`);
})();

Expected behavior

I create a date object via new Date(1960, 8, 24) and I insert it on both a date and a datetime attribute of a table record.

  • In the date attribute xdate I find “1960-09-23” which to me is unexpected behaviour.
  • In the datetime attribute xdatetime I find “1960-09-24” which I would expect.

Prisma information

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "mysql"
  url      = env("DATABASE_URL")
}

model myTable {
  id        Int       @id @default(autoincrement())
  name      String?   @db.VarChar(50)
  xdate     DateTime? @db.Date
  xdatetime DateTime? @db.DateTime(0)
}

Environment & setup

  • OS: MacOS 11.4
  • Database: MySQL 8.0.25.
  • Node.js version: v14.17.0

Prisma Version

2.24.1

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:5
  • Comments:13 (1 by maintainers)

github_iconTop GitHub Comments

16reactions
kazzkiqcommented, Apr 13, 2022

This is an issue most Prisma users will eventually stumble upon.

For example, I’m GMT-3 so if I do new Date("2022-04-12") it returns me Apr 11 2022 21:00:00. And if Prisma is forcing me to use new Date() every time I try to handle “YYYY-MM-DD” this would lead to really annoying timezone gymnastics in my code.

Prisma should not enforce JavaScript date type upon SQL Date fields exactly because of that.

Date types should definitely accept YYYY-MM-DD strings.

6reactions
pantharshit00commented, Jul 26, 2021

Thanks for the discussion here.

I also went through the issue and it seems to me this is not a serialization fault in Prisma as we just call toISOString() in the Date object that we receive which the engines component can parse and send to the database.

We can potentially allow just passing YYYY-MM-DD in native @db.Date so marking this as an improvement.

Read more comments on GitHub >

github_iconTop Results From Across the Web

4 Datetime Data Types and Time Zone Support
This chapter includes the following topics: ... The datetime data types are DATE , TIMESTAMP , TIMESTAMP WITH TIME ZONE, and TIMESTAMP WITH...
Read more >
Parse date without timezone javascript - Stack Overflow
I have the same issue. I get a date as a String, for example: '2016-08-25T00:00:00', but I need to have Date object with...
Read more >
Demystifying DateTime Manipulation in JavaScript - Toptal
Try to avoid creating a date from a string unless it is in ISO date format. Use the Date(year, month, date, hours, minutes,...
Read more >
Date & Time Data Types - Snowflake Documentation
TIMESTAMP_NTZ internally stores “wallclock” time with a specified precision. All operations are performed without taking any time zone into account. If the ...
Read more >
MySQL 5.7 Reference Manual :: 12.7 Date and Time Functions
See Section 11.2, “Date and Time Data Types”, for a description of the range of ... time values usually accept datetime values and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found