Back

[SOLVED] why the GMT changes while storing DateTime type value?

  • 0
  • Databases
  • Web
manazo
16 Apr, 2023, 21:54

the first attachment shows the date value that was passed to the DB. the second attachment shows the stored data in the DB. and the third data shows the data value that was returned by appwrite while listing

TL;DR
The user was experiencing an issue with the GMT changing when storing a DateTime value. The problem was that MariaDB (MySQL) Datetime type does not support timezones. When inserting a date into the database, MariaDB converts it to the server's current timezone. The solution is to convert the date each time it is retrieved from the database. For querying, use the server timezone (UTC in this case). The user tried different solutions but the issue was not resolved. It was suggested to use `return localDate.toISOString();` when sending dates to the database. The user provided code, but the issue was not resolved. It was
manazo
16 Apr, 2023, 21:55

the passed value is "23:59:59". but stored and returned value is "17:59:59"

Binyamin
16 Apr, 2023, 21:56

Okay. What I think happend is that the JavaScript convert it to your local timezone.

MariaDB remove the timezone so in timezone 0 this is indeed the time.

Binyamin
16 Apr, 2023, 21:56

A way around would be to be sure you don't have timezone when you send the JavaScript

manazo
16 Apr, 2023, 21:57

but i explicitly need timezones. cuz i'm working with different timezones than my local

Binyamin
16 Apr, 2023, 21:58

Mmm Let me check something real quick

manazo
16 Apr, 2023, 21:58

cool

Binyamin
16 Apr, 2023, 22:02

Can you show me the code in which your creating the document and inserting the date

manazo
16 Apr, 2023, 22:16

I have this storableData named big object. You can check the betDay prop's value in the first attachment. and the value is coming from another file that looks something like the second attachment, where I'm setting the time precisely to "23:59:59"

Binyamin
16 Apr, 2023, 22:30

Try to to something like this

TypeScript
 return localDate.toISOString();
Binyamin
16 Apr, 2023, 22:31

This way you'll return a full ISO date with time zone

Binyamin
16 Apr, 2023, 22:31

Use this method on any date you're sending to database

manazo
17 Apr, 2023, 08:46

i tried this(first attachment). also this (second attachment). but nothing is solved. just in case of return localDate.toISOString();, I got the milliseconds also added in the DB (third attachment), which wasn't getting added till now

Binyamin
17 Apr, 2023, 12:38

Okay, So I went over the code to double check, and this is actually what happens and what your workaround should be.

What actually happens? Appwrite using MariaDB (MySQL) Datetime type to store dates. And this type (Actually all MariaDB Date type) Don't support timezone.

When you inserting a new date to the db the MariaDB will convert it to the server current timezone.

That mean you get your time value store right but with a different timezone.

Postgres for example have a special type to store datetime with time zone

What you should do. This is a very common workaround in the world of MariaDB & MySQL. you will need to convert the date each time you're getting it from the database. And Appwrtie always returns the date timestamp

Inserting When you inserting or updating new date into the database to it as you use to.

Getting When you're getting (selecting) date attribute make sure to convert them

Querying When running quires with dates make sure to use the server timezone. In your case it's UTC.

Hope this gave you enough to workaround

manazo
18 Apr, 2023, 13:06

I've converted the date to utc and then queried the db with that date. the first attachment shows the iso-formatted date string, that I'm sending in the query. and the second attachment shows the date that my db have. as you can see, both are same, but it's still returning 0 documents (NOTE: i'm doing equal query)

Binyamin
18 Apr, 2023, 13:26

Try to change the date in the query to be something like that.

TypeScript
const date = new Date().toISOString().slice(0, 19).replace('T', ' ');
Binyamin
18 Apr, 2023, 13:26

Test it and lmn

manazo
18 Apr, 2023, 13:33

tested. still getting zero docs

Binyamin
18 Apr, 2023, 13:33

Can you share the code?

manazo
18 Apr, 2023, 13:35

sure. here

Binyamin
18 Apr, 2023, 13:37

And without the betDay it worked?

manazo
18 Apr, 2023, 13:52

yes. I've finally successfully discovered that I'm dumb and I should rethink my career choice. i was passing a Boolean value as a String

manazo
18 Apr, 2023, 14:01

thanks for your help btw

Binyamin
18 Apr, 2023, 14:02

👍

manazo
18 Apr, 2023, 14:03

[SOLVED] why the GMT changes while storing DateTime type value?

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more