-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DATEONLY field is being parsed as a javascript Date with time/zone #4858
Comments
Same in mysql too |
It's happening in Sequelize models/users.js:
routes/users.js:
Postman response:
console log:
Postgres select:
|
+1 |
@lahvey DATEONLY saves only the date part - If you want the best time as well, use DATE |
So the date part should be the date string date part, no matter what the timezone is, right? |
Yes - I was confused by the mention of times, so I thought you were expecting the field to store timestamp as well as date. |
Sequelize should return only date part for DATEONLY fields. I am using below workaround in my model definitions for all DATEONLY fields and it seems to be working fine so far:
|
I am constantly facing issues with this aswell. DATEONLY is timezone-independent on the database side, it should only return the date part as a string. The problem though is not in Sequelize. It is already in the drivers, for example postgres-types which is used by pg parses DATEONLY columns as |
I tried @mbhin's code snippet, but it returns me the previous day of the date stored in the database. The following function is working for me: function getDateWithoutTime(date) {
return require('moment')(date).format('YYYY-MM-DD');
} |
But seriously, |
@palodequeso Yes. Absolutely. I want to tackle this for 4.0 as it affects me and it's a breaking change. But it depends on driver implementation. Need to open an issue at postgres-date. |
This will be avaiable in sequelize 4.0? |
Yes, It will be. Its due in next release, hopefully end of this month. |
+one |
Any workaround to return date only, I tried @Poitrin way but its giving invalide date in date field |
this issue has been everywhere and existed from looong time ago... thanks @janmeier |
Could you elaborate what problem you have exactly? Please don't ping maintainers on random questions. We respond when we have time. |
DATEONLY ranges, defined as As per #9074, it seems that some type conversions are handled by
EDIT: Found the root of the problem and the solution |
And if we are not using Postgres? |
I have a similar problem, my dateonly in mssql returns a date always with one day less, I even created a question in the stack but still no answer. Sequelize: 5.7.6 |
Same problem in MSSQL using DATEONLY. As an example, when querying the database directly the date is '2019-06-01', but Sequelize returns '2019-05-31'. I'm in the US Central timezone, so I assume it is still treating it as a UTC date and converting it to my timezone. It is only returning the string in 'YYYY-MM-DD' format, which is good, but it is converting it based on my local time, which is bad. Sequelize: 5.8.5 |
Same Problem, DATEONLY type returns DATE complete from mysql |
Has anyone found workaround?
I tried mssql: 6 I suggest this workaround:
|
- Move signInEmail query to mutation with lacking useLazyQuery - Currently, useLazyQuery does not return promises apollographql/react-apollo#3499. - Change `status` to `statusMessage` and do migration - Fix some of migration scripts. * Add and update types for User - Add `isOnline`, `lastSignedIn` fields. - Changed to DATEONLY type for birthday and prevent being parsed to javascript date (sequelize/sequelize#4858).'
this work for me |
This isn't true any more, right? |
How is this still open after 5 years? |
@Vaelek because it's still a problem, at least it is for me when using Sequelize 5, MSSQL with date column (remote db, have no control to change it!). Also supplying {raw:true}/{timezone: "-5:00") doesn't work either. |
Can confirm that this is a problem. We're having the same issue with Sequelize 5 and MSSQL. We can use elements of the workarounds above, but this is far less than optimal in production. Any updates? |
My answer on StackOverflow for Postgres, but I duplicate it here. const pg = require('pg')
const { types } = pg
// we must store dates in UTC
pg.defaults.parseInputDatesAsUTC = true
// fix node-pg default transformation for date types
// https://github.com/brianc/node-pg-types
// https://github.com/brianc/node-pg-types/blob/master/lib/builtins.js
types.setTypeParser(types.builtins.DATE, str => str)
types.setTypeParser(types.builtins.TIMESTAMP, str => str)
types.setTypeParser(types.builtins.TIMESTAMPTZ, str => str) It's must be initialized before initiating your Sequelize instance. You can now run a query indicating the timezone for which you want to get the date. Suppose we make a SQL query, select all User's fields, and "createdAt" in timezone 'Europe/Kiev': SELECT "createdAt"::timestamptz AT TIME ZONE 'Europe/Kiev' FROM users WHERE id = 1;
# or with variables
SELECT "createdAt"::timestamptz AT TIME ZONE :timezone FROM users WHERE id = :id; For Sequelize (for User model) it will be something like this: sequelize.findOne({
where: { id: 1 },
attributes: {
include: [
[sequelize.literal(`"User"."createdAt"::timestamptz AT TIME ZONE 'Europe/Kiev'`), 'createdAt'],
// also you can use variables, of course remember about SQL injection:
// [sequelize.literal(`"User"."updatedAt"::timestamptz AT TIME ZONE ${timeZoneVariable}`), 'updatedAt'],
]
}
}); |
Does anyone know of a similar solution for |
If you're still experiencing this with Sequelize 5, you should update to Sequelize 6. Sequelize 5 is not maintained beyond security fixes anymore. |
I'm using Sequelize 6 with the 'postgres' dialect (Redshift).
Can anyone tell me how to get Dates from a Redshift database in an actually usable fashion? |
@armordog If you're using |
Update: I see it has nothing to do with Sequelize as it's It's still weird that the column Thanks, @ephys . All of them return a parsed JS Date. I'll make an SSCCE 👍 |
If your column type is actually |
I'll close this issue as I haven't had feedback that Sequelize 6 still returns DATEONLY as Date instances |
Plenty of reasons to want your DATEONLY fields as a string, but just wanted to chime in with a little tip for anyone getting very confusing incorrect dates due to the date conversion. At least this way the extra time info is just a nuisance and shouldn't cause any issues. One of the first things I always do when setting up a new project is make sure everything is running in UTC. Most deployed cloud/serveless/etc servers are likely to be set up this way by default already, so you're usually just making sure your local env and any edge cases are matching. To do this 1. Set your database timezone to UTCRun this SQL (for postgres) - 2. Set your running node process to operate in UTC
|
I am using postgres.
My dateonly field is being stored as YYYY-MM-DD but when I pull that data down to the client, it is being sent as a full timestamp with time and timezone parts.
The text was updated successfully, but these errors were encountered: