porsager / postgres

Postgres.js - The Fastest full featured PostgreSQL client for Node.js, Deno, Bun and CloudFlare
The Unlicense
7.26k stars 262 forks source link

Convert the Query Result Timestamp into mm/dd/yyyy #724

Closed chauhankiran closed 10 months ago

chauhankiran commented 10 months ago

Consider the following query.

return await sql`
  select
    *
  from tasks
  where
    id = ${id}
`;

This return the JSON result like this.

{
  "id": 1,
  "title": "Test 1",
  "createdOn": "2023-11-06T17:10:14.985Z",
}

I would like to return this createdOn result in mm/dd/yyyy format.

porsager commented 10 months ago

This is more of a general PostgreSQL question and not related to anything specific to this client. There are many ways to skin that cat 😉 You can google PostgreSQL date format for instance. Or if you need it displayed in JavaScript you can just pass the date returned to another little library of mine 😉 https://github.com/porsager/datie

aakashns commented 7 months ago

Is there a way to change the default date serialization behavior of the client?

I can see in https://github.com/porsager/postgres/blob/master/src/types.js#L31 that dates are serialized as follows:

serialize: x => (x instanceof Date ? x : new Date(x)).toISOString()

I'd like to change this to :

serialize: x => (x instanceof Date ? x : new Date(x)).getTime()

Is there a way to achieve this?

chauhankiran commented 7 months ago

I'm not sure but does this work @aakashns https://github.com/porsager/postgres/issues/161#issuecomment-801031062 ?