Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

189
Vistas
What is the data type for date and time in PostgreSQL?

I know the data types are TIMESTAMP, DATE, TIME, etc. I also know I should be using TIMESTAMP as it keeps track of any changes.

I am bringing the date from the front-end where in my react component the date is

const dateTime = new Date().toLocaleString("en-GB")

The output of which is "24/10/2021, 14:37:49"

If I set the data type to TIMESTAMP it gives me an error.

The question is what should be the data type to accept the above mentioned date and time format. I have tried DATETIME but it gives an error saying DATETIME does not exist. SQL query:

ALTER TABLE user_info_2 ADD date_col1 TIMEDATE;

I am new at PostgreSQL so any help is appreciated.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The issue is not the format per se, it is that the default Postgres Datestyle date ordering is:

--Note the MDY date ordering
show datestyle ;
 DateStyle 
-----------
 ISO, MDY

select '24/10/2021, 14:37:49'::timestamptz;
ERROR:  date/time field value out of range: "24/10/2021, 14:37:49"
LINE 1: select '24/10/2021, 14:37:49'::timestamptz;

---Change date ordering to put date first

set datestyle = iso,DMY;
show datestyle ;
 DateStyle 
-----------
 ISO, DMY
(1 row)

select '24/10/2021, 14:37:49'::timestamptz;
      timestamptz       
------------------------
 2021-10-24 14:37:49-07

So you have two choices:

  1. Change the DateStyle 'permanently' in postgresql.conf or temporarily per session using set datestyle = iso,DMY;

  2. Use the ISO format:

select '2021-10-24, 14:37:49'::timestamptz;
      timestamptz       
------------------------
 2021-10-24 14:37:49-07

I would say 2) is the preferred choice as Postgres does not store the formatting so it really does matter how you input the data. If the date ordering is important for your users then do the appropriate formatting on presenting the output to them.

about 4 years ago · Juan Pablo Isaza Denunciar

0

As I could see, using Date().toLocaleString() will return a string type. You should use a Date object to be persisted. For your column type in Postgres use TIMESTAMP.

typeof(Date().toLocaleString("en-GB"))

The output will be 'string'...

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda