Skip to content

Improve Timezone Support for Existing MySQL Databases configured with a Non-UTC Timezone #5051

Open

Description

@jhaemin

Bug description

The OS and MySQL time both are set to KST(Korea Standard Time) which is UTC+9:00.
But when create or update records with new Date(), DATETIME string doesn't respect the database's timezone and always filled with UTC time.

For example, when the current time is 2020-06-20 13:00:00 in Korea, I expect the data should be stored like below

id created_at
1 2020-06-20 13:00:00

However, what I get is UTC time which differs -9 hours.

id created_at
1 2020-06-20 04:00:00

How to reproduce

  1. Set the system and MySQL timezone to Asia/Seoul or UTC+9:00

  2. Create with new Date()

await prisma.myTable.create({
  data: {
    createdAt: new Date()
  }
})

Expected behavior

I expect the DATETIME string to be synced with MySQL's timezone.

Prisma information

model MyTable {
  createdAt DateTime @map(name: "created_at")

  @@map(name: "my_table")
}

Environment & setup

  • OS: Ubuntu
  • Database: MySQL 8.0.20
  • Prisma version: 2.0.1
  • Node.js version: 13.14.0

Activity

  1. transferred this issue fromprisma/ormon Jun 23, 2020
  2. pantharshit00 commented on Jun 23, 2020

    @pantharshit00
    Contributor

    We do automatic ISO conversions for JS Dates so you it will contains a UTC offset by default. If you want to provide a manual offset, you will need to provide a manual string by using something like https://date-fns.org/v2.8.1/docs/formatISO

    A manually offsetted date will look like this: 2020-05-05T16:28:33.983+09:00

    But I found a bug in our validator which I was triaging this so please follow: prisma/prisma-client-js#741

  3. pantharshit00 commented on Jun 23, 2020

    @pantharshit00
    Contributor

    (Internal note: I can reproduce this but I think this is a usage error which is unfortunately blocked by a bug prisma/prisma-client-js#741. So I am labeling as bug/1-repro-available for now)

  4. jhaemin commented on Jun 23, 2020

    @jhaemin
    Author

    @pantharshit00 Yes, currently I'm inserting data with manually adjusted time (+9:00). The problem of doing this is that I always have to subtract 9 hours again from a result of Prisma.

  5. pantharshit00 commented on Jun 23, 2020

    @pantharshit00
    Contributor

    Try after prisma/prisma-client-js#741 is fixed. You can use a simple library like date-fns to get a correct offset.

    https://date-fns.org/v2.8.1/docs/formatISO

    So

    import { formatISO } from 'date-fns'
    
    const date = formatISO(new Date()) // date will have +09:00 if system uses that timezone
  6. divyenduz commented on Jul 27, 2020

    @divyenduz
  7. janpio commented on Sep 2, 2020

    @janpio
  8. jhaemin commented on Sep 2, 2020

    @jhaemin
    Author

    Okay, prisma/prisma-client-js#741 is not fixed yet so I cannot use the method @pantharshit00 suggested. Using the value of formatISO(new Date()) throws an error.

    ...
    createdAt: '2020-09-03T00:38:06+09:00'
               ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ...
    Argument createdAt: Got invalid value '2020-09-03T00:38:06+09:00' on prisma.createOneTip. Provided String, expected DateTime.
    

    And unfortunately the error I issued is still happening. Prisma create or update with new Date() does not respect the system or Database timezone and always store the time string as UTC.

    Here's the thing. Suppose the time in Korea is 2020-09-03 13:00.

    MySQL timezone is set to Korean standard time(KST, +09:00). NOW() and CURRENT_TIMESTAMP both correctly display 2020-09-03 13:00 which means optional time fileds are automatically filled with KST time string in DB. But when I get the data with Prisma, it treats the time string as UTC and store the Date object inside the result. This mismatched time makes me always subtract 9 hours from the result.

    My system timezone is also set to KST as well. date command shows Thu Sep 3 13:00:00 KST 2020 correctly.

  9. added a commit that references this issue on Sep 2, 2020
  10. pantharshit00 commented on Sep 2, 2020

    @pantharshit00
    Contributor

    Thanks @jhaemin for the update. I have candidated the other issue again so will discuss it soon for the next patch.

  11. timsuchanek commented on Jan 5, 2021

    @timsuchanek
    Contributor

    Thanks a lot for reporting 🙏
    This issue is fixed in the latest dev version of @prisma/cli.
    You can try it out with npm i -g @prisma/cli@dev.

    In case it’s not fixed for you - please let us know and we’ll reopen this issue!

  12. jhaemin commented on Jan 5, 2021

    @jhaemin
    Author

    @timsuchanek

    Right now, I've tested with @prisma/cli@dev and @prisma/client@dev.

    Tested time is 10:55 PM (KST).

    Screen Shot 2021-01-05 at 10 54 31 PM

    Here is the tested Prisma schema model.

    model test {
      id       Int      @id @default(autoincrement())
      saved_at DateTime @default(now())
    }
    

    And I ran the codes below

    await prisma.test.create({
      data: {}, // no `saved_at` input because it's optional
    })

    and

    await prisma.test.create({
      data: {
        saved_at: new Date(), // pass `new Date()` explicitly
      },
    })

    And here is the result in mysql.

    Screen Shot 2021-01-05 at 10 58 25 PM

    And when I create a row with MySQL's CURRENT_TIMESTAMP,

    image

    The datetime field which is set to CURRENT_TIMESTAMP is not correctly resolved to MySQL's CURRENT_TIME. The times created by Prisma is UTC, which is -9 hours from KST.

    It's not fixed, and it should be reopened.

  13. Jolg42 commented on Jan 6, 2021

    @Jolg42
    Contributor

    Reopening to reproduce the latest comment @pantharshit00

  14. reopened this on Jan 6, 2021
  15. 160 remaining items

  16. anaibol commented on Oct 20, 2024

    @anaibol

    +1

  17. kiwavi commented on Oct 25, 2024

    @kiwavi

    Still exist until today wtf🤣move to drizzle guys, better performance 😅 we can insert and select with our timezone custom type

    Never used drizzle before. Just checked out the docs. Wouldn't default(sqlnow()) just work? With regards to respecting the timezone set in the DB?

    Update. Tested this on drizzle. It works!

  18. chinaBerg commented on Dec 4, 2024

    @chinaBerg

    I broke down!!! Due to timezone issues, Prisma's business cannot be used in many regions. Perhaps its idea is good, but it is too idealistic, so it can only be abandoned.

  19. samimozcan commented on Jan 8, 2025

    @samimozcan

    is there any update ?

  20. yyzclyang commented on Apr 1, 2025

    @yyzclyang

    It's April 1, 2025 now, and it still hasn't addressed this issue. Maybe I should consider Drizzle

  21. partap commented on Jul 23, 2025

    @partap

    I've got the same issue, but my database is SQL Server, which Drizzle doesn't support yet :(
    Back to handwritten SQL for me, I guess...

  22. MAY-ZHONGYE commented on Aug 25, 2025

    @MAY-ZHONGYE

    It's August 25, 2025 now, and it still hasn't addressed this issue.

  23. AnantaPodder commented on Sep 3, 2025

    @AnantaPodder

    Would love to migrate to drizzle. bye prisma.

  24. cod378 commented on Sep 18, 2025

    @cod378

    Hi, the same problem with dates to MS SQL.

  25. elechen commented on Oct 11, 2025

    @elechen

    #use mariadb adapter, it provides timezone config

    npm install @prisma/adapter-mariadb

    make sure to set MySQL session time_zone: +00:00, which match PRISMA's behavior "We do automatic ISO conversions for JS Dates"

    const adapter = new PrismaMariaDb({
      host: "localhost",
      port: 3306,
      connectionLimit: 5,
      timezone: 'Z',
      database: 'your_database',
      user: "xxx",
      password: "xxx",
    });
    const prisma = new PrismaClient({ adapter });
    
  26. felicienfrancois commented on Oct 17, 2025

    @felicienfrancois

    @Rexpull please don't polute this thread with IA generated content which is partly wrong.

    • The issue is on Mysql, not Postgre
    • The issue comes from date conversion (SQL<=>Javascript). This is the role of the database driver which should apply timezone offset based on database timezone or a timezone driver setting. This is a standard feature, all database drivers do it properly except prisma legacy one. It should be fixed there.

    Every middleware based solutions are missing a part of the thing (for example if a field is tagged as @default(now()) or @createdat, the date may be set automatically by prisma to a wrong value without being corrected by middlewares).

    Then the solution is that prisma fix the mysql prisma driver or provide a way to use widely used existing drivers (mysql2, mariadb) without wierd side effects.
    The last solution may already be possible with adapters but I don't know about it (I dropped prisma 3 years ago because of this issue)

  27. oldTimer98 commented on Nov 25, 2025

    @oldTimer98

    请问什么时候可以解决一下这个问题,已经五年了

  28. JYbill commented on Jan 16, 2026

    @JYbill

    #use mariadb adapter, it provides timezone config#使用 mariadb 适配器,它提供了时区配置

    npm install @prisma/adapter-mariadb

    make sure to set MySQL session time_zone: +00:00, which match PRISMA's behavior "We do automatic ISO conversions for JS Dates"确保设置 MySQL 会话 time_zone: +00:00,这与 PRISMA 的行为 "我们为 JS 日期自动进行 ISO 转换" 相匹配

    const adapter = new PrismaMariaDb({
      host: "localhost",
      port: 3306,
      connectionLimit: 5,
      timezone: 'Z',
      database: 'your_database',
      user: "xxx",
      password: "xxx",
    });
    const prisma = new PrismaClient({ adapter });
    

    This is indeed a temporary solution because prisma-client by default converts the new Date() into the format of the zero-timezone. This ensures correct handling of timestamps that are not in UTC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions