postgres icon indicating copy to clipboard operation
postgres copied to clipboard

Returning the postgres DATE type

Open StefanHoutzager opened this issue 4 years ago • 0 comments

Currently I get my dates (defined with datatype DATE) back as f.e. "2021-03-02T23:00:00.000Z" while pgadmin gives 2021-03-03. So I get the wrong date back (and a timestamp was added). You can find the same issue in node-postgres https://github.com/brianc/node-postgres/issues/818 where someone suggests a workaround in the end (that I just tested, it works). https://github.com/brianc/node-postgres/issues/818 , see a paste of the snippet below. Can this be fixed in deno-postgres? My proposal would be to change decodeDate in decode.ts to simply

function decodeDate(dateStr: string): Date {
    return new Date(dateStr + 'T00:00:00Z');
}

of course that works for year 0001 too. ;-) For null values in the postgress date the function decodeDate is not executed.

/* Node postgres date fix /
var Moment = require('moment');
var parseDate = function parseDate(val) {
return val === null ? null : Moment(val).format('YYYY-MM-DD')
};
var types = pg.types;
var DATATYPE_DATE = 1082;
types.setTypeParser(DATATYPE_DATE, function(val) {
return val === null ? null : parseDate(val)
});
/ Node postgres date fix - end */

StefanHoutzager avatar Oct 07 '20 11:10 StefanHoutzager