Save the Date.now () no postgres

Hello, I would like to know what type of data I use in the time column of my table, which will store the Date.now() of Javascript, it returns an integer like this 1591181717431. I thought about using big int, does anyone have a better idea?

Author: Evilmaax, 2020-06-03

2 answers

This integer is the amount of milliseconds that have passed from the date of 01/01/1970 to 00:00:00 to the current moment. Formally known as was Unix .

Postgres is able to convert an integer representing the Unix Era in seconds to type TIMESTAMP by means of the function to_timestamp(), see only:

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));

Querying Table:

SELECT id, datahora FROM tb_foobar;

Output:

| 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
Author: Lacobus, 2020-06-03 19:38:21

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 like this:

alter table tabela alter column datahora set default current_date; 

That way every time you enter a record it will take the time and date of the system and save in the record

Https://stackoverflow.com/questions/10211145/getting-current-date-and-time-in-javascript

 1
Author: João Vitor Fontes, 2020-06-03 19:44:43