Node-postgres: Q: passing Date() variable as parameter to client.query()

Created on 13 Oct 2016  路  2Comments  路  Source: brianc/node-postgres

Question: What are best practices for passing Date objects as _client.query()_ parameters for _TIMESTAMP WITH TIME ZONE_ and _TIMESTAMP WITHOUT TIME ZONE_ values?

Also, when passing a Date object as a _TIMESTAMP WITH TIME ZONE_ value, is getTimezoneOffset() used to synthesize the actual time zone passed on the wire to postgres?

question

Most helpful comment

I just pass them. Simply. Just query("SELECT $1::timestamptz", [myDate]) (or timestamp when not using a time zone).

You can see the date parse implementation here: https://github.com/brianc/node-pg-types/blob/master/lib/binaryParsers.js#L132
In the code you can see that only when using timestamptz the timezone within is used. If using timestamp (without timezone), only date and time is stored and the timezone is assumed to be the local one. So when storing in postgres the timezone is lost, and when getting from postgres the local timezone is assumed.

I advise to _always_ use timestamps with time zone. It is very useful information and fixes (and prevents) problems when time zones differ, either on the client side, or between pg client and pg server.

All 2 comments

I just pass them. Simply. Just query("SELECT $1::timestamptz", [myDate]) (or timestamp when not using a time zone).

You can see the date parse implementation here: https://github.com/brianc/node-pg-types/blob/master/lib/binaryParsers.js#L132
In the code you can see that only when using timestamptz the timezone within is used. If using timestamp (without timezone), only date and time is stored and the timezone is assumed to be the local one. So when storing in postgres the timezone is lost, and when getting from postgres the local timezone is assumed.

I advise to _always_ use timestamps with time zone. It is very useful information and fixes (and prevents) problems when time zones differ, either on the client side, or between pg client and pg server.

I advice _not_ to always use timestamp with time zone, but instead to learn the difference between timestamp with time zone and timestamp without time zone and to use the correct tool for the job.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tonylukasavage picture tonylukasavage  路  4Comments

ClueLessEggHead picture ClueLessEggHead  路  3Comments

v1co1n picture v1co1n  路  4Comments

chovy picture chovy  路  3Comments

Cosrnos picture Cosrnos  路  3Comments