javascriptpostgresql-11

Convert from JS timestamp to Postgresql Timestamp with time zone


I have a simple t = Date.now; which goes to Postresql t_time column declared as timestamp with time zone via REST API. I got the error:date/time field value out of range.

I can't use the answer from this post as I don't write raw SQL but use ORM(Knex.Js). Calling db only to make the conversion looks like an overkill to me. Any other solution in js?

How come I can't find a convertor? Am I missing something?

Note: I just need date and time as hh:mm:ss, I don't need milliseconds.


Solution

  • Date.now() only provides the since EPOCH in milliseconds.

    You need an ISO date time string. To achieve that, set t like this:

    const t = new Date(Date.now()).toISOString();
    console.log(t);