Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 882
SharpBCD Avatar asked Jun 29 '19 19:06

SharpBCD


People also ask

How does Postgres store timestamp with timezone?

The TIMESTAMP (also known as TIMESTAMP WITHOUT TIME ZONE ) and TIMESTAMPTZ (also known as TIMESTAMP WITH TIME ZONE ) types stored as a 64-bit integer as a microsecond offset since 1970-01-01 in CRDB and as a 64-bit integer microsecond offset since 2000-01-01 in PostgreSQL (by default).

Does Postgres timestamp have timezone?

Introduction to PostgreSQL timestamp The timestamp datatype allows you to store both date and time. However, it does not have any time zone data. It means that when you change the timezone of your database server, the timestamp value stored in the database will not change automatically.

Does Postgres store dates as UTC?

PostgreSQL assumes your local time zone for any type containing only date or time. All timezone-aware dates and times are stored internally in UTC .

How do I convert DateTime to date in PostgreSQL?

PostgreSQL Convert DateTime to Date Using EXTRACT() Function EXTRACT() function takes in two arguments, field and source. The field argument specifies the date part, i.e. month, decade, hour, to extract from the DateTime value. The source argument is a value of type TIMESTAMP or INTERVAL.


1 Answers

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);
like image 177
Randy Casburn Avatar answered Sep 28 '22 07:09

Randy Casburn