MySQL DateTime Unexpected behavior - javascript

I have a DATETIME field in my table that is being displayed differently than what I'm returning.
To understand, here are the exact object fields I'm returning in my API:
However, when I'm using PostMan and looking at the results, the time is being changed from what I'm returning to this:
I don't understand what's going on. I'm trying to store the DATETIME in the database as UTC time, and then have it sent over to the client, where the client will convert it to local time.
Here's what the DATETIME dates looks like in the database:

You're not storing the dates in UTC time. "GMT-0400 (EDT)" means that they're being stored in Eastern time. The Z in the other results means UTC (or Zulu) time, according to this spec: http://www.ietf.org/rfc/rfc3339.txt
I expect that your MySQL installation has the default timezone set to the timezone that your box is in (https://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html) so when you add a time without specifying the timezone, it's assumed it's in the default timezone. That is then being converted to UTC in your response, adding four hours to the time.
You should probably specify a timezone when adding information to the database, and/or change the default timezone for your database. There may also be another layer "helpfully" changing things, depending on the API you're using to access the database, so if there is relevant information about the API, you can add that to your question.

Related

Converting UTC timestamp in column to user's time in Postgres VS on client side

I have a message table schema like this (msg_id, msg_from_id, msg_to_id, msg_text, msg_time).
...and I'm thinking about the conversion of the message timestamp.
Store the user's timezone in the users table, do an AT TIME ZONE = 'SELECT tz FROM users where ...' while retrieving the messages.
Retrieve the data just as is and convert on the client side
What's the better approach between the two?
The simple and correct solution is to set the database parameter timezone correctly for the client session:
SET timezone = 'Asia/Kolkata';
Then the database will render all timestamp with time zone correctly for that time zone. In short, PostgreSQL can do the time zone management for you.

How to change the format of the Timestamp type from outputted postgresql query?

I'm trying to create a website to monitor the air quality in my room and I want to monitor the time where each measurement was taken. The problem is that when I get my data from my database and print it on my front end, the format of the time column is different from the database. This is the format of the time in the database:
2022-05-16 11:41:00+02
This is all fine and dandy, it contains the correct date and time, and it tells me that my timezone is UTC+2 (Denmark/Copenhagen). But when I need to use this exact date/time on my client, it is printed like this:
2022-05-16T09:41:00.000Z
So it changes the format by inserting the T and the .000Z in the end, as well as subtracting 2 from the hours. How do I change this formatting? Ideally I would like it to look like this:
16-05-2022 11:41
Do I need to define this in my query, or do I have to do it on my server? And how do I do it?
Good afternoon,
You could do like this:
SELECT to_char(current_TIMESTAMP (0) AT TIME ZONE 'Denmark/Copenhagen', 'DD-MM-YYYY HH:MM');

How to handle dates on Firestore?

From what I understand, when you store a date:
const date = firebase.firestore.Timestamp.fromDate(new Date())
You are storing a date object in the local timezone. However, when I later read this date from Firestore using:
doc.data().date.toDate()
This gives me the date in UTC, instead of the local timezone. Is this the expected behaviour? Am I understanding this correctly? It seems like it would make more sense to retrieve the date in local time since the date you stored is in local time.
When you store a date (in Firestore) with
firebase.firestore.Timestamp.fromDate(new Date()), you are storing a
date object in the local timezone.
This is actually not correct: All timestamps in Firestore are saved in UTC. The timezone of the user who saved the timestamp is not saved with the timestamp value.
Is this the expected behaviour?
Yes, storing the value in UTC gives the maximum flexibility for the developer to display it as desired, depending on his specific requirements.
As a matter of fact, it's up to the developer to display it with the timezone he wants when displaying it in a frontend. For example, the Firebase console converts the UTC date the timezone to which your computer is configured.

Javascript console logged DATE type POSTGRESQL reduced 1 day

I have a database with date as the Id and im going to select the date/id of the last 5 days from my postgresql database to browser using command bellow and send it to the client side using res.send on node/express
SELECT * FROM ratings ORDER BY rating_id DESC LIMIT 5
but if i console log it, it always less 1 day behind, 2021-04-02 even not in the database
i have done parsed it from the client side, but what i got is only the last 4 days, 2021-04-03 is missing
This is because your data field has a timezone component. So my question is: do you need store the timezone component in your dates?
If yes. So when you format your time you need take it in consideration. The browser automatically go to give you the date in the browser time zone, for that may be you can get a day more or less.
If no, you can format the date without take the timezone component.
PD: you should store your date values as UTC date in your DB always.

Manage daylight saving time when sending datetimes from javascript to php

I need to store datetimes in a mysql database.
These datetimes come from the browser, so users in the US, Europe or Asia or wherever will see their correct times.
In the browser, i have the following javascript code:
var d = new date();
var iso_date_string = d.toISOString();
// produces something like "2017-02-17T19:42:27.100Z"
I send this string to the server which runs PHP, and in my server code I have:
$date = date( "Y-m-d H:i:s", strtotime("2014-02-17T19:42:27.100Z") );
And then I insert this value in MySQL.
Everything works fine, except I don't get the exact time, there is a 1 hour difference between what I store in MySQL and the time I have on my computer.
I think this may be related to taking into account daylight saving times, but I can't find how to do it.
Please advise.
When you always send the UTC datetime to your server and let javascript convert it back when showing, the problem will not occur.
The PHP date will be affected by the Timezone information embedded in the php.ini file.
As far as mysql storage is concerned, you will probably want to use UTC or some other standard not affected by daylight savings to store your datetime in mysql. Then, regardless of which browser reads it, it should get converted back according to their local timezone information.
See this article for further reading:
Daylight saving time and time zone best practices
You can use getTimezoneOffset() to check if it's DST, because it corrects for DST.
I found this solution on
http://javascript.about.com/library/bldst.htm
Setting the correct timezone information in the php.ini file did the trick.
Thanks.

Categories

Resources