0
Hello, I would like to know what kind of data I use in the time column of my table, which will store the Date.now()
from Javascript, it returns an integer like this 1591181717431. I thought to use the big int, someone has a better idea?
0
Hello, I would like to know what kind of data I use in the time column of my table, which will store the Date.now()
from Javascript, it returns an integer like this 1591181717431. I thought to use the big int, someone has a better idea?
1
This whole number is the amount of milliseconds that have passed since the date of 01/01/1970
at 00:00:00
to date. Formally known as Unix age.
Postgres is capable of converting an integer representing the Unix Age in seconds to the type TIMESTAMP
through the function to_timestamp()
, look at you:
CREATE TABLE tb_foobar
(
id INTEGER,
datahora TIMESTAMP
);
INSERT INTO tb_foobar (id, datahora) VALUES (1, to_timestamp(1591181717431 / 1000));
INSERT INTO tb_foobar (id, datahora) VALUES (2, to_timestamp(1591182735732 / 1000));
INSERT INTO tb_foobar (id, datahora) VALUES (3, to_timestamp(1581182718337 / 1000));
Consulting Table:
SELECT id, datahora FROM tb_foobar;
Exit:
| id | datahora |
|----|----------------------|
| 1 | 2020-06-03T10:55:17Z |
| 2 | 2020-06-03T11:12:15Z |
| 3 | 2020-02-08T17:25:18Z |
See working on Sqlfiddle
1
Use
new Date().toLocaleString();
But I recommend that if you want to save the date that the record is entered in the bank, I would do so:
alter table tabela alter column datahora set default current_date;
this way every time you enter a record it will pick up the time and date of the system and save in the record
https://stackoverflow.com/questions/10211145/getting-current-date-and-time-in-javascript
Browser other questions tagged postgresql timestamp
You are not signed in. Login or sign up in order to post.
And if instead of taking the
Date.now()
you catch thenew Date()
and store as Timestamp in the bank?– Denis Rudnei de Souza
I saw a people talking that it is very good to study timestamp and UTC, I think I will do what Denis said. Vlw. Grateful.
– Sérgio Avilla
By chance there is some particularity in its application that makes the
Date.now()
of Javascript other thannow()
Postgresql? If you don’t have to put as DEFAULT clause in the field of your table.– anonimo